DATA Step, Macro, Functions and more

IF/Then/Let in Macro

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

IF/Then/Let in Macro

Hello,

I have a quick question regarding setting up some Macros for a code I am trying to run. Is it possible to create a macro, and then do a statement along the likes of If Macro = This Then Let Macro2 = This?

I did some research, and I may have the code, but I'm not entirely sure how to use it. Could any one shed some light on what I may be missing?

My code is below. I want the user to select 'ALIGNED' or 'NOT ALIGNED' in the beginning of the code, and based on that selection, the code will update the year_add macro to be either 2013 or 2012,2013.

%let align_status = ('ALIGNED');

/*%let align_status = ('NOT ALIGNED');*/

%if &align_status.= 'ALIGNED' %then %let year_add = (2013);

%if &align_status. = 'NOT ALIGNED' %then %let year_add = (2012,2013);

I think the %if/%then can't be in an open code, but I have no idea how to put these into another macro and get that macro to run.

Thanks in advance for your help!

Tony


Accepted Solutions
Solution
‎06-26-2013 02:55 PM
Super User
Super User
Posts: 6,499

Re: IF/Then/Let in Macro

Since you had not created the macro variable YEAR_ADD before you called the macro YEAR the value was stored in the local symbol table for the macro YEAR and so it was no longer available after the macro ends. Simplest solution is to define it before calling the macro.

%let align_status = 'ALIGNED';

%let year_add = ;

%year ;

....  where year_add in &year_add ....


You will get more flexibility by removing the commas and parenthesis from your macro variables. They are not needed for use in IN operator lists.


%let year_add = 2012 2013 ;

.... where year_add in (&year_add) ....


View solution in original post


All Replies
Super Contributor
Posts: 333

Re: IF/Then/Let in Macro

I think you may be miss using terms. To me it sounds like you are setting up macro variables and need a way to selective set other macro variables. To do this you should be able to write a real sas macro that will do this. See if the following works:

%macro set;

%if &align_status = 'ALIGNED' %then %do;

     %let year_add = (2013);

%end;

     %else %do;

          %let year_add = (2012,2013);

     %end;

%mend set;

%set;

%put &align_status  &year_add;

New Contributor
Posts: 3

Re: IF/Then/Let in Macro

Thanks for your help. It appears we are getting somewhere, but it is still not working.

My code now reads:

%let align_status = 'ALIGNED';

/*%let align_status = 'NOT ALIGNED';*/

%LET start_dt = '01JAN2012'D;

%LET END_dt = '31DEC2012'D;

%MACRO YEAR;

%if &align_status.= 'ALIGNED' %then %do;

%let year_add = (2013);

%end;

%if &align_status. = 'NOT ALIGNED' %then %do;

%let year_add = (2012,2013);

%end;

%MEND YEAR;

%YEAR;

%put &align_status &year_add;

My error comes up when the year_add macro is needed:

121        where year_add in &year_add.

                             _

                             22

WARNING: Apparent symbolic reference YEAR_ADD not resolved.

121        where year_add in &year_add.

                             _

                             76

ERROR 22-322: Syntax error, expecting one of the following: (, SELECT. 

ERROR 76-322: Syntax error, statement will be ignored.

I tried the exact same code above, and I get the same error. Is there something else we are missing?

Solution
‎06-26-2013 02:55 PM
Super User
Super User
Posts: 6,499

Re: IF/Then/Let in Macro

Since you had not created the macro variable YEAR_ADD before you called the macro YEAR the value was stored in the local symbol table for the macro YEAR and so it was no longer available after the macro ends. Simplest solution is to define it before calling the macro.

%let align_status = 'ALIGNED';

%let year_add = ;

%year ;

....  where year_add in &year_add ....


You will get more flexibility by removing the commas and parenthesis from your macro variables. They are not needed for use in IN operator lists.


%let year_add = 2012 2013 ;

.... where year_add in (&year_add) ....


New Contributor
Posts: 3

Re: IF/Then/Let in Macro

Awesome thanks! I believe that was the issue - I didn't define the year_add macro before the macro YEAR. My code now works!

Thank you both!

My working code now reads:

%let align_status = 'ALIGNED';

/*%let align_status = 'NOT ALIGNED';*/

%let year_add = ;

%MACRO YEAR;

%if &align_status.= 'ALIGNED' %then %do;

  %let year_add = (2013);

  %end;

%if &align_status. = 'NOT ALIGNED' %then %do;

  %let year_add = (2012, 2013);

  %end;

%MEND YEAR;

%YEAR;

%put &align_status &year_add;

Super User
Super User
Posts: 6,499

Re: IF/Then/Let in Macro

Your post is a little confusing because it sounds like you are using the word MACRO to refer to macro variables instead on an actual macro (defined by %MACRO / %MEND statements).

To execute conditional logic statements such %IF/%THEN/%ELSE you would need to define a macro and then invoke it to get it to run.

Note that I have removed the parenthesis and commas from your macro variable values.  You probably do not need or want them anyway.

You could define a "function" style macro to help with the conversion. Such as this:

%macro year_add(status);

   %if "&status" = "ALIGNED" %then 2013;

   %else 2012 2013 ;

%mend year_add;

Then you could use it like this:

%let align_status=ALIGNED;

%let year_add=%year_add(&align_status) ;

Or you could use %SYSFUNC() to call the IFC() function instead.

%let year_add=%sysfunc(ifc("&align_status"="ALIGNED",2013,2012 2013));

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 238 views
  • 5 likes
  • 3 in conversation