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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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