I would like to generate a new variable that is set equal to an element from a macro variable list. But, I want to select the element from the list based on a value in the dataset.
For example:
%let myvar = 12 23 95 1005;
data temp;
do x = 1 to 4;
output;
end;
run;
data temp2;
set temp;
y=%scan(&myvar,x,' ');
run;
This code yields an error because the %scan function is expecting an integer for the second condition.
I want the output to be a dataset:
x y
1 12
2 23
3 95
4 1005
Any thoughts or suggestions would be appreciated. Thank you!
Hello @cminard,
Use the SCAN function instead:
y=scan("&myvar",x,' ');
or simply
y=scan("&myvar",x);
Hello @cminard,
Use the SCAN function instead:
y=scan("&myvar",x,' ');
or simply
y=scan("&myvar",x);
You need to use the SCAN() function in a data step. %SCAN() is for the macro processor, not for SAS code.
data temp2;
set temp;
y=scan("&myvar",x,' ');
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.