Question on referencing do loop variables as macro variables in a datastep

Reply
Frequent Contributor
Posts: 105

Question on referencing do loop variables as macro variables in a datastep

Hi!

 

So I want to reference a SAS do loop variable as a macro variable, so that I can call different variables within the dataset based on what the loop variable is. I.e:

 

I create macro variables containing the data I want for each "Scenario":

 

data scenarios;
input scenario $ ;
datalines;
Base
Adverse
Stress
;
proc sql;
select scenario
into :scenarios1-:scenarios3
from scenarios;
quit;
%put &&scenarios1;
%put &&scenarios2;
%put &&scenarios3; /*Just to verify they are populated as I expect */

 

Then I have a dataset with variables named by scenario, i.e. "pred_var_base", "pred_var_adverse" etc. I want to loop through and evaluate them in each row using the array/do loop so I don't have to manually write out statements for each scenario:

 

data want_data;

set have_data;

do i = 1 to 3;

pred_var_&&scenarios&i. = calculated_var_&&scenarios&i. * 100;

end;
run;

 

However it will not let me reference the "i" variable with the & in that way (get the error "Apparent symbolic reference I not resolved"), so I can't access the appropriate "Scenario" based on where I am in the do loop. Is it possible to do this in any way?

 

Thank you

 

Super User
Posts: 5,099

Re: Question on referencing do loop variables as macro variables in a datastep

There are many people on this message that could help ... if you clarify the question.  Since there are only a handful of variables to consider, try spelling out what the final DATA step would look like if you had to hard-code it (no macro language allowed).  Once you have done that, you will likely get a boatload of viable solutions.

Trusted Advisor
Posts: 1,631

Re: Question on referencing do loop variables as macro variables in a datastep

[ Edited ]

This should be done inside a macro

data want_data;
    set have_data;
    %do i = 1 %to 3;
    pred_var_&&scenarios&i. = calculated_var_&&scenarios&i. * 100;
    %end;
run;

 

 

Super User
Posts: 17,963

Re: Question on referencing do loop variables as macro variables in a datastep

Isn't that better done in an array than a macro? Is there a specific reason for using macros?
Trusted Advisor
Posts: 1,631

Re: Question on referencing do loop variables as macro variables in a datastep


Reeza wrote:
Isn't that better done in an array than a macro? Is there a specific reason for using macros?

Yes, that works too. I'm not sure I see that macros are superior to arrays, or vice versa.

Respected Advisor
Posts: 3,908

Re: Question on referencing do loop variables as macro variables in a datastep

@PaigeMiller

My rule-of-thumb is to only use macro code if it can't be done using normal Base SAS code. That's why I'd consider using arrays the better solution from a coding perspective. It's certainly easier to read, maintain and debug. 

Trusted Advisor
Posts: 1,631

Re: Question on referencing do loop variables as macro variables in a datastep

Yes, that makes sense.

 

In the simple example given above, an array would definitely work and would be the best solution. If the situation were more dynamic, and you did not know what values of SCENARIO would be present, a macro seems like it would be the way to go.

Ask a Question
Discussion stats
  • 6 replies
  • 345 views
  • 0 likes
  • 5 in conversation