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

Hi Everyone,

I need to perform a check of macro variable (&n1 and &n2) value inside %DO loop.

So with count=1, the Macro will not return anything since &n&count turns to &n1 which has value of 'aa'

With count=2, Macro will run.

Can you please help to fix that IF statement below?

Thank you,

HHCFX


%let n1=aa;
%let n2=bb;

%Macro check(count=);
	%IF &n&count!='aa' %then %do;
			%put code working;
	%end;
%mend;

%check(count=1); 
%check(count=2); 
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Try this:

%let n1=aa;
%let n2=bb;

%Macro check(count=);
	%IF &&n&count.=aa %then %do;
			%put code working;
	%end;
%mend;

%check(count=1); 
%check(count=2); 

The &&var&othervar is called "indirect reference". In effect the the first 2 && resolve to one & and holds that while the other bits  var&othervar resolve, then use &<resolved>.

Note that your comparsion with 'aa' was NEVER going to work without providing quotes in the values as the macro language works differently than the data step. EVERYTHING in the macro language is text so quotes are seldom needed unless the value has to resolve with a quote (and be prepared for lots of headaches getting them to work).

View solution in original post

2 REPLIES 2
ballardw
Super User

Try this:

%let n1=aa;
%let n2=bb;

%Macro check(count=);
	%IF &&n&count.=aa %then %do;
			%put code working;
	%end;
%mend;

%check(count=1); 
%check(count=2); 

The &&var&othervar is called "indirect reference". In effect the the first 2 && resolve to one & and holds that while the other bits  var&othervar resolve, then use &<resolved>.

Note that your comparsion with 'aa' was NEVER going to work without providing quotes in the values as the macro language works differently than the data step. EVERYTHING in the macro language is text so quotes are seldom needed unless the value has to resolve with a quote (and be prepared for lots of headaches getting them to work).

hhchenfx
Rhodochrosite | Level 12

Thank you so much!

HHC

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 670 views
  • 0 likes
  • 2 in conversation