Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why if statement is not working in sas macro?

I wanna get back one character by calling this macro but there is an error:

%macro getcategory(date=);

%global category;

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

%if %year(date) < 2002 %then %do;
%let category = A;

%mend;

%getcategory(date=1999);

I tried with symput but not working.

>Solution :

It looks like you are trying to use the %year() function to determine the year of the date parameter that is passed to the getcategory macro. However, the %year() function is not part of the SAS language.

To get the year from a SAS date value, you can use the year() function, which is part of the SAS date and time functions. Here is an example of how you might use it:

%macro getcategory(date=);
  %global category;

  %let year = %sysfunc(year(date));
  %if &year < 2002 %then %do;
    %let category = A;
  %end;
%mend;

%getcategory(date=1999);

In this example, the %sysfunc() function is used to call the year() function within the macro. This is necessary because the year() function is a SAS function, not a macro language function.

Note that the year() function returns the year as a four-digit number, so you will need to compare it to the value 2002 rather than the value 2.

I hope this helps! Let me know if you have any other questions.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading