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

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;

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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