简体   繁体   中英

SAS Macro variable resolve issue in datastep

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?

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM