SAS Macro variable resolve issue in datastep

vaibhav mehta :

There are similar named macro variables like temp1,temp2 etc..

temp1=xyz; temp2=abc;

Now I want to store these macro variable values into dataset.

I tried writing something as below:

%let n=2;
data current_data;
do=1 to &n.;
myvalues="&&temp&i.";
run;

But its not working. It seems that i is not getting resolved in the same dataset as it is declared in just like call symput function does.

Can anyone help?

Tom :

To dynamically retrieve the value of a macro variable at run time in a data step use symget() function, the reverse of the call symputx() function. You can use cats() to help you build the macro variable name from your integer value.

data current_data;
  do=1 to &n.;
    length myvalues $200;
    myvalues=symget(cats('temp',i));
    output;
  end;
run;

You could use macro logic to generate some wallpaper code. Note that you must define a macro to use %DO loop.

%macro expand;
   data current_data;
     length i 8 myvalues $200;
  %do i=1 %to &n;
     i=&i;
     myvalues="&&temp&i";
     output;
  %end;
   run;
%mend expand;
%expand;

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=376199&siteId=1