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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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