Reputation: 2917
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.
Upvotes: 1
Views: 549
Reputation: 1763
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)
Upvotes: 4