BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tk9077
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

5 REPLIES 5
esjackso
Quartz | Level 8

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;

tk9077
Calcite | Level 5

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?

Tom
Super User Tom
Super User

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) ....


tk9077
Calcite | Level 5

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;

Tom
Super User Tom
Super User

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));

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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