BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SAS_inquisitive
Lapis Lazuli | Level 10

I want to store values of macro variable "&macvar" in a data set.

 

%macro m;

	proc sql noprint;
		select distinct name
			into: macvar separated by ' '
		from sashelp.class;
	quit;

	%put &macvar;

	data _null_;
		%do i= 1 %to 19;
			%put %scan(&macvar,&i,' ');
		%end;
	run;

%mend m;

%m
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

OK, still not too difficult.  Here's the basic idea:

 

data want;

length value $ 32;

do i=1 to countw("&macvar");

   value = scan("&macvar", i, ' ');

   output;

end;

run;

 

I'm not sure if there might be a set of special characters that COUNTW would treat as delimiters (besides a blank).  You might have to modify the DO statement to account for that, if &MACVAR might contain special characters.

View solution in original post

6 REPLIES 6
Astounding
PROC Star

If you want the individual NAME values stored separately, you don't need the macro variable:

 

proc sql;

create table want as select distinct name from sashelp.class;

quit;

 

If you want the entire string stored as one long DATA step variable, you could use:

 

data want;

value = "&macvar";

run;

SAS_inquisitive
Lapis Lazuli | Level 10

@Astounding. I was trying to process the list (&macvar) and output each value in a data set (such as using  OUTPUT statement).

Astounding
PROC Star

OK, still not too difficult.  Here's the basic idea:

 

data want;

length value $ 32;

do i=1 to countw("&macvar");

   value = scan("&macvar", i, ' ');

   output;

end;

run;

 

I'm not sure if there might be a set of special characters that COUNTW would treat as delimiters (besides a blank).  You might have to modify the DO statement to account for that, if &MACVAR might contain special characters.

SAS_inquisitive
Lapis Lazuli | Level 10

@Astounding.  Thank you. This is what I was looking for.

LinusH
Tourmaline | Level 20
Assuming that this for learning purpose only since the operation of creating a macro variable from a table, and then create a new table with its values is just crazy.
Or is there a real requirement that is not shown by your example?
As stated, can't recall any real world case where I did such an operation.
But technically, you need to scan the the variable in a do while/until loop and do explicit output.
Data never sleeps
SAS_inquisitive
Lapis Lazuli | Level 10
@ LinusH. This is just an example. But I know being able to process list (either horizontal or vertical) is an important skill.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 6 replies
  • 9136 views
  • 3 likes
  • 3 in conversation