如何在SAS Proc SQL中将多行值连接成一行

时间:2015-09-03 19:56:57

标签: sql sas concatenation proc-sql

我有以下数据。我想保留唯一的make变量,并将有关独特汽车制造商的所有信息连接成一行,如下所示:

!!我只想使用Proc SQL来完成所有工作。

欲望输出:

AMC Concord 22 2012-02-19; Pacer 17 2010-09-14; Spirit 22 2011-08-17 
Buick Century 20 2001-12-19; Electra 15 2003-08-02  
Cuick Electra 15 2002-10-01; Century 20 2007-05-30; Spirit  22 2004-11-03 

代码:

DATA cars1;
INPUT make $ model $ mpg date YYMMDD10.;
CARDS;
AMC Concord 22 2012-02-19 
AMC Pacer   17 2010-09-14 
AMC Spirit  22 2011-08-17 
Buick Century 20 2001-12-19  
Buick Electra 15 2003-08-02  
Cuick Electra 15 2002-10-01  
Cuick Century 20 2007-05-30  
Cuick Spirit  22 2004-11-03 
;
RUN;

data cars2;
set cars1;
date = put(date,yymmddn8.);
run;

到目前为止我尝试过的是:

proc sql;
create table test as
    select  distinct make,model, CATX (",", model,mpg) AS var1,
                        CATX (",", CALCULATED var1, put(date,10.)) AS var2,
                        monotonic() as rownum,
                        max(calculated rownum) as max,min(calculated rownum) as min,
    case 
        when calculated rownum=calculated max then 2  
        when calculated rownum=calculated min then 1 
        else 0 
    end as record,

    case
        when calculated record = 1 then CATS (calculated var2) 
        else CATX (",", CATS (calculated var2),calculated var2) 
    end as result

from cars2
group by make
order by rownum;
quit;

0 个答案:

没有答案