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

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.

Nivas88
Calcite | Level 5

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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