DATA Step, Macro, Functions and more

concatenate values from data set row into one string (variable number of columns)

Reply
Contributor
Posts: 60

concatenate values from data set row into one string (variable number of columns)

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?

Super Contributor
Posts: 1,636

Re: concatenate values from data set row into one string (variable number of columns)

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 cSmiley Happy;

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

Contributor
Posts: 60

Re: concatenate values from data set row into one string (variable number of columns)

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

Super Contributor
Posts: 1,636

Re: concatenate values from data set row into one string (variable number of columns)

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 cSmiley Happy;
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

Ask a Question
Discussion stats
  • 3 replies
  • 363 views
  • 3 likes
  • 2 in conversation