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;
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.