PROC SQL:collect_set或group_concat是否等效?

时间:2018-10-18 15:35:45

标签: sas proc-sql

给出以下数据,

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;

我正在寻找此输出:

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

来自Hive / Apache Spark的世界,collect_set + concat_ws之类的功能运行良好,但我在SAS中找不到等效功能。

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

类似的答案如下:Can I Comma Delimit Multiple Rows Into One Column? [duplicate]。分组行聚合和串联。

1 个答案:

答案 0 :(得分:2)

proc sql没有该功能,但是您可以在datastep中使用first来实现。最后。

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;