如何汇总一列数据并用分隔符分隔值?

时间:2014-11-12 09:19:02

标签: sas

我需要从output1转换为output2

第一栏是我打算如何分手。第二列是我想要汇总的内容。将汇总值与/分开。

输出1

data output1;
    input id $ app $;
    datalines;
    id001 app11
    id001 app12
    id002 app21
    id002 app22
    id002 app23
    id003 app31
    id003 app32
    id004 app41
    ;

输出2:

id001 app11/app12
id002 app21/app22/app23
id003 app31/app32
id004 app41

非常感谢

5 个答案:

答案 0 :(得分:2)

您可以使用

PROC TRANSPOSE DATA=TRAIL1 OUT=TTRAIl1(DROP=_NAME_) PREFIX=COL;
    VAR app; BY id; 
RUN;

%LET NCOLS = 3; *you can get this from VTABLE

DATA RES(DROP=i COL1 - COL&NCOLS.);
    SET TTRAIl1;
    LENGTH RES $256; 
    ARRAY COLS{*} COL1 - COL&NCOLS.; 
    RES = COL1;
    DO i = 2 TO DIM(COLS);
       IF MISSING(COLS(i)) eq 0 THEN RES = CATS(RES,'/',COLS(i));
    END;
RUN;

答案 1 :(得分:2)

试试这个:

data testin;
 input id $ app $;
 datalines;
 id001 app11
 id001 app12
 id002 app21
 id002 app22
 id002 app23
 id003 app31
 id003 app32
 id004 app41
 ;
 run;

 data new;
   length app $ 50;
   length app0 $ 50; 
   set testin ;
     if id0 eq id then app = trim(app)||'/'||trim(app0);
   id0 = id;
   app0 = app;
   retain id0 app0;
 run;
 proc sort data=new; by descending app0; run;
 proc sort data=new (DROP=app0 id0) nodupkey; by id; run;

答案 2 :(得分:0)

这也会奏效。     数据输出1;      输入id $ app $;      datalines;      id001 app11      id001 app12      id002 app21      id002 app22      id002 app23      id003 app31      id003 app32      id004 app41      ;      运行;

 /*allows the next data step to use first. last. processing for the retain*/
 proc sort data=output1;
 by id app;
 run;

data output2 (drop=app rename=(app2 = app));

 set output1;
 length app2 $24.;
 /*allows the value of app2 to be held to the next observation*/
 retain app2;
 /*begins first/last processing in the data step*/
 by id;

if first.id then app2 = app;
else app2 = catx('/',app2,app);
/*if you comment the next line you can see what the if/else is doing*/
if last.id;

run;

答案 3 :(得分:0)

您可以使用以下代码

proc transpose data = TRAIL1 out=trail_trans(DROP=_NAME_) prefix=COL_ ;
    var app; by id; 
run;

data output2(keep=id out_var);
length out_var $500.;
set trail_trans;
    COL_temp="";                               *this temp column is to handle if there is no data in 'app'(column) and next line throws error;
    out_var = catx ("/",OF COL:);
run;

答案 4 :(得分:0)

从我的手机写这个,所以对于缺少缩进道歉。

proc sort data=trail1;
by id app;
run;

data want (drop=app);
do until (last.id);
set trail1;
by id;
length apps $100;
apps=catx('/', apps, app);
end;
run;