1

Given the following data,

data tmp;
   input class $ age gpa graduated;
   datalines;
    A 16 2.47 1
    B 13 3.65 1
    A 13 2.04 0
    B 12 2.3 0
    C 15 3.58 1
;
run;

I'm seeking this output:

class|unique_ages
A|13, 16
B|12, 13
C|15

Coming from the world of Hive/Apache Spark, functions like collect_set + concat_ws have worked well, but I haven't found the equivalent in SAS.

PROC SQL;
    SELECT
        class
        * some grouping function on `age`;
    FROM tmp
        GROUP BY class
    ;
QUIT;

Similar answers look like this: Can I Comma Delimit Multiple Rows Into One Column? [duplicate]. Group-by row aggregation and concatenation.

blacksite
  • 12,086
  • 10
  • 64
  • 109

1 Answers1

3

proc sql does not have that functionality but you can do it in datastep using first. and last.

data tmp;
input class $ age $  gpa graduated;
datalines;
A 16 2.47 1
B 13 3.65 1
A 13 2.04 0
B 12 2.3 0
C 15 3.58 1
;
run;


 proc sort data=tmp out =have;
 by class age;
 run;

data want(keep = class group_ages);
set have ;
by class;
retain group_ages;
length group_ages $20.;
if first.class then group_ages = ' ';
else group_ages= catx(',', group_ages, age);
if last.class then output;
run;
Kiran
  • 3,255
  • 3
  • 14
  • 21