BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
cminard
Quartz | Level 8

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!

 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @cminard,

 

Use the SCAN function instead:

y=scan("&myvar",x,' ');

or simply

y=scan("&myvar",x);

View solution in original post

2 REPLIES 2
FreelanceReinh
Jade | Level 19

Hello @cminard,

 

Use the SCAN function instead:

y=scan("&myvar",x,' ');

or simply

y=scan("&myvar",x);
Tom
Super User Tom
Super User

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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1258 views
  • 4 likes
  • 3 in conversation