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;
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.
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.
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!
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.