Help using Base SAS procedures

Group Values in a variable

Reply
Contributor
Posts: 27

Group Values in a variable

I need to group values in  variable - and Arrive at the following Output

Input -

Header 1Header 2Header 3
Sub 1101102
Sub 1124125
Sub 1175176
Sub 2201202
Sub 2275276
Sub 2295296


Output -

Header 1Header 2
Sub 1101, 102, 124, 125, 175, 176
Sub 2201, 202, 275, 276, 295, 296


Thanks in Advance.

Super Contributor
Posts: 1,636

Re: Group Values in a variable

Hi madhu,

I am sure there are better ways to solve your problem. I will post my code anyway.

data have;

input header1 $ var1 var2;

cards;

Sub1 101 102

Sub1 124 125

Sub1 175 176

Sub2 201 202

Sub2 275 276

Sub2 295 296

;

run;

proc transpose data=have out=want21 ;

by header1;

var var1;

run;

proc transpose data=have out=want22 prefix=n;

by header1;

var var2;

run;

data want(keep=header1 header2) ;

  merge want21 want22;

  by header1;

     array v(*) col1-col3 n1-n3;

     call sortn(of v(*));

       header2=catx(',',of v(*));

  run;

Valued Guide
Posts: 2,174

Group Values in a variable

and in a single pass:

data want (keep= header1 header2 compress=yes ) ;

do until( last.header1 ) ;

   set have ;

   by header1 ;

   length header2 $32760 ;

  header2 = catx( ',', header2, var1, var2 ) ;

end ;

run ;

Super Contributor
Posts: 1,636

Re: Group Values in a variable

Thank you Peter! have a nice weekend!

Contributor
Posts: 27

Group Values in a variable

Thanks Peter and Linlin

Ask a Question
Discussion stats
  • 4 replies
  • 121 views
  • 0 likes
  • 3 in conversation