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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
sbxkoenk
SAS Super FREQ

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

View solution in original post

5 REPLIES 5
sbxkoenk
SAS Super FREQ

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

MART1
Quartz | Level 8

many thanks @sbxkoenk 

 

all understood, now works a treat!

 

thanks

 

Tom
Super User Tom
Super User

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;
MART1
Quartz | Level 8

thanks @Tom 

 

yes completely missed calling the macro.

 

no reason why I put it in quotes; noted for the next time!

 

thanks

 

 

 

PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1366 views
  • 0 likes
  • 4 in conversation