DATA Step, Macro, Functions and more

Concatenating too many variables

Accepted Solution Solved
Reply
Regular Contributor
Posts: 243
Accepted Solution

Concatenating too many variables

Hi All,

I need to concatenate almost 100 variables and I don't want to put a comma (,) after each one of them as it is too much of work. My variables has a sequence and it follows the pattern like var1, var2, var5, var11......

 

Is there a way to concatenate all of them with a short code like new_Var=CAT(var1-var100)

 

Here is how my data looks like.

DATA Have;
INPUT VAR1 VAR2$ VAR5 VAR10$;
DATALINES;
107 ENG1 10 NY
105 Bio2 15 ON
101 Che1 10 TX
107 Bio1 11 DL
105 Eng2 15 CA
100 Phy2 11 MR
105 Che1 15 DC
;
RUN

 

Can someone help me please.

 

Thanks,

 


Accepted Solutions
Solution
‎03-01-2018 04:12 PM
Super User
Posts: 22,820

Re: Concatenating too many variables

CATX and a variable list. This will include all varialbes that start with the prefix VAR:

Depending on your naming convention you may need other options, google "SAS 9.4 variable lists" to see all the different ways of using abbreviations in your variable lists.

 

want = catx(", ", of var:);

Note, this works only in a data step since SQL doesn't support variable lists.

View solution in original post


All Replies
Solution
‎03-01-2018 04:12 PM
Super User
Posts: 22,820

Re: Concatenating too many variables

CATX and a variable list. This will include all varialbes that start with the prefix VAR:

Depending on your naming convention you may need other options, google "SAS 9.4 variable lists" to see all the different ways of using abbreviations in your variable lists.

 

want = catx(", ", of var:);

Note, this works only in a data step since SQL doesn't support variable lists.

Regular Contributor
Posts: 243

Re: Concatenating too many variables

Thanks Reza, this is so helpful. Do you think if there is a way to bring to concatenated data as separate line instead of side by side? Like for the first observation it will come like:
New_Var
107
ENG1
10
NY
Super User
Posts: 22,820

Re: Concatenating too many variables

It depends on the final output. SAS doesn't have a way of specifying line breaks in a variable that I know of...and I would probably leave that to my reporting step. 

 

What is your final output destination? If it's HTML, perhaps a line break symbol may be needed, or if its RTF the appropriate tag would be needed. 

 

 

Regular Contributor
Posts: 243

Re: Concatenating too many variables

Hi Reeza,
My final destination file is Excel. Thanks.
Super User
Posts: 22,820

Re: Concatenating too many variables

Then that's the ALT+ENTER key you need and you can probably add that in. It won't show in the cell, and I'm not 100% sure Excel would recognize it properly. 

 

 

If you use ODS EXCEL you could go down the formula route and use CHAR(10) or "0A"x...

 

https://excel.tips.net/T002788_Simulating_Alt_Enter_in_a_Formula.html

 

 

PROC Star
Posts: 2,215

Re: Concatenating too many variables

or 

NEWVAR = cat(of VAR1-VAR100);

if you want to use the explicit list that you had.

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 130 views
  • 2 likes
  • 3 in conversation