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
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.
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;
@Astounding. I was trying to process the list (&macvar) and output each value in a data set (such as using OUTPUT statement).
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.
@Astounding. Thank you. This is what I was looking for.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.