Hello
I'm trying to create an if statement in a macro.
I've used examples found in this forum but I cannot get it to work.
Below is my dummy example
%let source = TABLE.TEST1;
%put &source;
%macro test;
%if &source eq TABLE.TEST1 %then %let header = &source;
%else %let header = 'TABLE.TEST2';
%mend test;
%put &header;
but it does not assign any value to &header (the log says "Apparent symbolic reference not resolved" ).
I really don't see what am doing wrong?
Thanks
Two things are missing:
1. defining your macro is not enough, you need to call it as well (after macro definition has run)!
2. you need to make your macro variable header global, otherwise it is not known outside the macro.
Here's corrected code:
%let source = TABLE.TEST1;
%put &=source;
%macro test;
%GLOBAL header;
%if &source eq TABLE.TEST1 %then %let header = &source;
%else %let header = 'TABLE.TEST2';
%mend test;
%test
%put &=header;
Koen
Two things are missing:
1. defining your macro is not enough, you need to call it as well (after macro definition has run)!
2. you need to make your macro variable header global, otherwise it is not known outside the macro.
Here's corrected code:
%let source = TABLE.TEST1;
%put &=source;
%macro test;
%GLOBAL header;
%if &source eq TABLE.TEST1 %then %let header = &source;
%else %let header = 'TABLE.TEST2';
%mend test;
%test
%put &=header;
Koen
You never called the macro. Also if you do not already have a macro variable named HEADER then the %LET statements in the macro will create a LOCAL macro variable which will disappear when the macro finishes running.
One simple fix is to just define the macro variable HEADER before calling the macro. Also why are you putting quotes into HEADER sometimes and not other times?
%macro test;
%if &source eq TABLE.TEST1 %then %let header = &source;
%else %let header = TABLE.TEST2;
%mend test;
%let source = TABLE.TEST1;
%let header = ;
%test;
%put &=header &=source;
thanks @Tom
yes completely missed calling the macro.
no reason why I put it in quotes; noted for the next time!
thanks
In addition to the above useful advice, also there is almost never a good reason for enclosing the value of a macro variable in quotes or double-quotes. So
%else %let header = 'TABLE.TEST2';
probably should be
%else %let header = TABLE.TEST2;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: