DATA Step, Macro, Functions and more

storing macro variable values in data set

Accepted Solution Solved
Reply
Super Contributor
Posts: 271
Accepted Solution

storing macro variable values in data set

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

Accepted Solutions
Solution
‎03-31-2016 02:42 PM
Super User
Posts: 5,516

Re: storing macro variable values in data set

Posted in reply to SAS_inquisitive

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


All Replies
Super User
Posts: 5,516

Re: storing macro variable values in data set

Posted in reply to SAS_inquisitive

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;

Super Contributor
Posts: 271

Re: storing macro variable values in data set

Posted in reply to Astounding

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

Solution
‎03-31-2016 02:42 PM
Super User
Posts: 5,516

Re: storing macro variable values in data set

Posted in reply to SAS_inquisitive

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.

Super Contributor
Posts: 271

Re: storing macro variable values in data set

Posted in reply to Astounding

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

Super User
Posts: 5,437

Re: storing macro variable values in data set

Posted in reply to SAS_inquisitive
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
Super Contributor
Posts: 271

Re: storing macro variable values in data set

@ LinusH. This is just an example. But I know being able to process list (either horizontal or vertical) is an important skill.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 341 views
  • 3 likes
  • 3 in conversation