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

Hello,

 

I have a sas work dataset as below ( only one column)

 

ID

12

34

56

78

 

I need to assign these ID's again into a single variable as like below

 

%let = "12","34","56","78";

 

I tried with catx funtion to convert multiple rows into single row but didnot help. Please help me to achieve this.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The simplest way to make a macro variable from data is using PROC SQL and in the INTO keyword.

So to make a macro variable named LIST from the values of ID in the dataset HAVE you could use something like this:

proc sql noprint;
select distinct quote(trim(ID),"'") into :list separated by ',' 
  from have
;
quit;

What to you plan to DO with the macro variable?  Do you really need the commas?  The IN operator in SAS does not mind if you use spaces instead of commas between the items in the list.  Spaces in macro variables are much easier to work with than commas.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Why do you need this? What is the benefit? What will you do next once you have this variable?

 

By the way, are you talking about a MACRO variable? You didn't actually say that, but you use %LET.

--
Paige Miller
ballardw
Super User

Is the value of ID duplicated in the data set?

If so should there be two or more copies in the data set?

Does the order of the values have to match the order they appear in the data set?

Is the variable numeric or character?

How many values are we talking about? There is a limit on the number of characters in a macro variable and adding basically 3 characters for each value means you start cutting into the number of values that will even fit.

 

Almost every time we see a "place all the values of a variable in a data set into a macro variable" we see a follow up with attempts to use that variable in a manner that is very inefficient, cumbersome or just plain fails. So a clear description of what is to be done with this may be more helpful.

Tom
Super User Tom
Super User

The simplest way to make a macro variable from data is using PROC SQL and in the INTO keyword.

So to make a macro variable named LIST from the values of ID in the dataset HAVE you could use something like this:

proc sql noprint;
select distinct quote(trim(ID),"'") into :list separated by ',' 
  from have
;
quit;

What to you plan to DO with the macro variable?  Do you really need the commas?  The IN operator in SAS does not mind if you use spaces instead of commas between the items in the list.  Spaces in macro variables are much easier to work with than commas.

freshstarter
Obsidian | Level 7

Thanks All for the suggestion. didnot realise proc sql has an easy way out. 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 4 replies
  • 549 views
  • 0 likes
  • 4 in conversation