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

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
Barite | Level 11

Thank you so much!

HHC

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 564 views
  • 0 likes
  • 2 in conversation