SAS如何在1个观察组中连接来自不同观察结果的变量

时间:2014-09-22 14:44:11

标签: dataset sas

我有不同地址的学生,每次观察1次,我想把所有地址放在学生的1次观察中

我有这个:

data students;
input  name : $50.  address $50.;
datalines;
Ron St Jeremy
Ron Central Market
Edna St Thomas
John Av. Washington
John St Jeremy
Ron  Dolphin 541
;
run;

我想要这个数据集:

Name  address
Ron   St Jeremy, Central Market, Dolphin 541
Edna  St Thomas
John  Av. Washington, St Jeremy

我该怎么做?

2 个答案:

答案 0 :(得分:2)

如果订单不重要(不确定我理解你的评论)那么你可以这样做:

proc sort data = students out = students_srted;
 by name;
run;

data concat;
 set students_srted;
 by name;
 retain concat_address;
 if first.name then do; concat_address = address; end;
  else do; concat_address = concat_address || address; end;
 if last.name then do; output; end;
run; 

这将连接您的名字,但是不允许重复数据或订购,您需要提供更详细的信息

答案 1 :(得分:2)

这样的事情就足够了:

data students;
input  name : $50.  address $50.;
datalines;
Ron St Jeremy
Ron Central Market
Edna St Thomas
John Av. Washington
John St Jeremy
Ron  Dolphin 541
;
run;

proc sort data = students;
          by name;
run;

data want;
length cat $250.;
   do until (last.name);
      set students;
        by name notsorted;
      cat=catx(', ',cat,address);
   end;
   drop address;
run;

如果没有地址顺序指示器,则无法在其后面创建特定的逻辑规则。

EDITED ANSWER @ user3142441非常正确,首先需要排序 - 在撰写答案时错过了这一点。