BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
baroche64
Fluorite | Level 6
I'm trying to use a %do loop to replicate what the commented out code below does which is to simply set up some counters for 5 crops. The commented code works perfectly, but how do I achieve the same more efficiently using a %DO?
I tried using &TopCrop&i.._Farm but that doesn't work either, what am I doing wrong? Thanks for helping.

if Second_Level_Validation eq "Pass" or Main_Farm_Commodity in ("&TopCrop1.","&TopCrop2.","&TopCrop3.","&TopCrop4.","&TopCrop5.") then do; %do i = 1 %to 5; if Main_Farm_Commodity eq "&TopCrop&i." then &TopCrop&i._Farm = 1; else &TopCrop&i._Farm = 0; %end; /* if Main_Farm_Commodity eq "&TopCrop1." then &TopCrop1._Farm = 1; else &TopCrop1._Farm = 0; if Main_Farm_Commodity eq "&TopCrop2." then &TopCrop2._Farm = 1; else &TopCrop2._Farm = 0; if Main_Farm_Commodity eq "&TopCrop3." then &TopCrop3._Farm = 1; else &TopCrop3._Farm = 0; if Main_Farm_Commodity eq "&TopCrop4." then &TopCrop4._Farm = 1; else &TopCrop4._Farm = 0; if Main_Farm_Commodity eq "&TopCrop5." then &TopCrop5._Farm = 1; else &TopCrop5._Farm = 0; */ end;
1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

To indirectly address macro variables, use a double ampersand:

&&TopCrop&i.

In the first round of resolving, the two ampersands are reduced to one, and the macro variable i is resolved, so it is now

&TopCrop1

(if &i was 1)

In the second round, &topcrop1 is resolved as usual.

View solution in original post

2 REPLIES 2
Kurt_Bremser
Super User

To indirectly address macro variables, use a double ampersand:

&&TopCrop&i.

In the first round of resolving, the two ampersands are reduced to one, and the macro variable i is resolved, so it is now

&TopCrop1

(if &i was 1)

In the second round, &topcrop1 is resolved as usual.

baroche64
Fluorite | Level 6
Thanks Kurt that worked! using the double ampersand and two dots:

%do i = 1 %to 5;
if Main_Farm_Commodity eq "&&TopCrop&i.." then &&TopCrop&i.._Farm = 1;
else &&TopCrop&i.._Farm = 0;
%end;

SAS Innovate 2025: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 504 views
  • 2 likes
  • 2 in conversation