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

I have the following example data set:

data test;
  test = "A123"; output;
  test = "B123"; output;
run;

I would like to to assign the values A123 and B123 into a macro so that the result would be the same as :

%let readin=("A123" "B123")

Then I would use &readin. in data step's where statement.

1 ACCEPTED SOLUTION

Accepted Solutions
Quentin
Super User

One common way to read a list of values from a dataset variable into a macro variable is to use PROC SQL:

 

proc sql noprint ;
  select quote(test) into :readin separated by " " 
  from test
  ;
quit ;

%put &readin ;

This will return:

"A123" "B123"

If you want to include the parentheses in the value, I think the easiest way is to add them with a %LET statement after the PROC SQL step:

proc sql noprint ;
  select quote(test) into :readin separated by " " 
  from test
  ;
quit ;
%let readin=(&readin) ;

%put &readin ;

View solution in original post

3 REPLIES 3
Quentin
Super User

One common way to read a list of values from a dataset variable into a macro variable is to use PROC SQL:

 

proc sql noprint ;
  select quote(test) into :readin separated by " " 
  from test
  ;
quit ;

%put &readin ;

This will return:

"A123" "B123"

If you want to include the parentheses in the value, I think the easiest way is to add them with a %LET statement after the PROC SQL step:

proc sql noprint ;
  select quote(test) into :readin separated by " " 
  from test
  ;
quit ;
%let readin=(&readin) ;

%put &readin ;
PaigeMiller
Diamond | Level 26

Data, like these two text values, is much more easily handled in a SAS data set. It is much less easily handled in a macro. While there are reasons why a macro may be the best choice, those reasons are rare. We could give you better advice if we knew what you are planning to do with these character strings. Please tell us what you are going to do next with these character strings?

 

A minor comment about terminology: you are not creating a macro, you care creating a macro variable. Macro variables are not the same as macros.

--
Paige Miller
trevand
Obsidian | Level 7

I am using it to load in only a subset of a huge data set. So in the data step I restrict using where=(variable in (@readin.))

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 950 views
  • 1 like
  • 3 in conversation