简体   繁体   中英

How to create variables in data step and set to macro variable values

I have a data step as follows.

    %macro do();
    select temp into :templist separated by "|" from Filenames ;
    select temp1 into :templist1 separated by "|" from Filenames ;

    %do i = 1 %to &count.;
          data sub;
              set sub;
               temp =%scan(&templist,&i,|);
               temp2 =%scan(&templist1,&i,|);
               %end
             run;
          proc append base= master data= sub;
          run;
    %end;
  %mend;

I want to create two new variables temp and temp2 and set them to macro variable values. I know how to do the reverse, that is, create a macro variable in the data step, but I was wondering how I create a new variable in the data step and set that variable to the value of a macro variable.

If I got it right, you want to create a new dataset with two variables temp and temp2 so that i-th row contains i-th elements of macrovariables &templist and &templist1 . Then, you can do it in this way:

%let count=3;
%let templist=a|b|c;
%let templist1=x|y|z;


%macro loop;
%do i = 1 %to &count.;
    temp ="%scan(&templist,&i,|)";
    temp2 ="%scan(&templist1,&i,|)";
    output;
%end;
%mend loop;


data want;
    %loop
run;

You don't need to put into macro loop the entire DATA-step - only lines where you assign variables. BTW, you can't use %DO-loop per se - only inside a macro. And make notice, that %SCAN should be in double quotes, so that when macro-code executes and creates open code, on the right side of the assigning statements

temp ="%scan(&templist,&i,|)";

and

temp2 ="%scan(&templist1,&i,|)";

there would appear character expression ( "a" , "b" etc , not a , b etc)

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