Skip to content

Instantly share code, notes, and snippets.

@ymattu
Created October 19, 2016 16:11
Show Gist options
  • Save ymattu/e07c2de1c809c30cffc1117066e1f91b to your computer and use it in GitHub Desktop.
Save ymattu/e07c2de1c809c30cffc1117066e1f91b to your computer and use it in GitHub Desktop.
SASでSQLのgroup_concat的なことをやる
/*https://communities.sas.com/t5/SAS-Enterprise-Guide/How-to-use-GROUP-BY-to-concatenate-strings-in-SAS-proc-SQL/td-p/208967*/
data want (drop=string_old);
set tmp (rename=(string=string_old));
by id;
retain string;
length string $ 20; * set large enough to accommodate the maximum number of records per ID;
if first.id then string = '';
string = catx(',',trim(string),string_old);
if last.id then output;
run;
data original(index=(Cust_no));
input Cust_no Prod $ ACC_flag $ Bal $ ;
datalines;
111 a1 qaz 10
111 b1 wsx 11
222 c1 wsx 233
333 d1 grw 251
444 e1 dc 25
444 f1 vefw 1231
444 g1 wefr 36
555 h1 bdfgh 615
666 i1 qe 767
777 j1 erg 31
777 k1 eth 472
777 l1 ret 251
777 m1 dfwef 3761
888 p1 ffef 36
run;
proc sort data=original(keep=cust_no) out=customers nodupkey;
by Cust_no;
run;
data new;
set customers;
length Prod_con Acc_con $50;
do until (_iorc_ ne 0);
set original key = Cust_no;
if _iorc_ = 0 then do;
Prod_con = CATX('|',Prod_Con,Prod);
Acc_con = CATX('|',Acc_con,Acc_flag);
end;
end;
output;
_iorc_ = 0; _error_=0;
run;
proc print;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment