Hi,
I use the posted code to generate new vars, but after running the code, I just got one new variable, y20, what's wrong with my code?
Thanks a lot!
%macro abc;
%do i=1 %to 20;
data code1;
set code;
y&i=x&i/2;
run;
%end;
%mend;
%abc
It is possible to use macro language to generate 20 assignment statements within the same DATA step. You just need to rearrange the pieces of your program:
%macro abc;
data code1;
set code;
%do i=1 %to 20;
y&i=x&i/2;
%end;
run;
%mend abc;
%abc
You always overwrite dataset code1, so only the results of the last macro iteration take effect.
Thank you so much Kurtbremser!
More importantly, why are you creating 20 datasets for the same data? This uses more disk space, is slower, and is far harder to maintain. I am pretty certain after seeing this type of code a lot, you will then either be merging those datasets, or looping over them using further messy macro code. Don't. You can simply do one step:
data code1 (drop=i); set code; array y{20} 8.; array x{8}; /* assumes it already exists in code */ do i=1 to 20; y{i}=x{i}/2; end; run;
Or you could create a normalised version of the above with each item in a row rather than columns which is a better structure.
It is possible to use macro language to generate 20 assignment statements within the same DATA step. You just need to rearrange the pieces of your program:
%macro abc;
data code1;
set code;
%do i=1 %to 20;
y&i=x&i/2;
%end;
run;
%mend abc;
%abc
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.