BookmarkSubscribeRSS Feed
tom12122
Obsidian | Level 7

I have data set X that can have variable number of columns named c1, c2, c3 etc cn.

I need to concatenate them in one string. I need to have code that works regardless what the number of those columns is.

Is there a way to concatenate with some c: wildcard or similat to that?

3 REPLIES 3
Linlin
Lapis Lazuli | Level 10

Tom,

how about:

data have;

input id (c1-c5)($);

cards;

1 a b c d e

2 d r b t t

;

data want;

length newvar $ 50;

set have;

newvar=catt(of c:);

proc print;run;

            Obs    newvar    id    c1    c2    c3    c4    c5

                  1     abcde     1    a     b     c     d     e

                  2     drbtt      2    d     r     b     t     t

Linlin

tom12122
Obsidian | Level 7

Thanks. But is there a possibility to add some separator or space between values?

Linlin
Lapis Lazuli | Level 10

yes.

data have;
input id (c1-c5)($);
cards;
1 a b c d e
2 d r b t t
;
data want;
length newvar $ 50;
set have;
newvar=catx('+',of c:);
run;
proc print;run;

Obs     newvar      id    c1    c2    c3    c4    c5

1     a+b+c+d+e     1    a     b     c     d     e
2     d+r+b+t+t      2    d     r     b     t     t

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1031 views
  • 3 likes
  • 2 in conversation