10-08-2015 12:13 PM
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":
input scenario $ ;
%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:
do i = 1 to 3;
pred_var_&&scenarios&i. = calculated_var_&&scenarios&i. * 100;
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?
10-08-2015 12:21 PM
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.
10-08-2015 12:24 PM - edited 10-08-2015 01:06 PM
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;
10-08-2015 01:08 PM
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.
10-08-2015 07:07 PM
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.
10-09-2015 08:59 AM
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.