Proc Transpose每个组具有多个ID值

时间:2018-06-13 17:50:27

标签: sas transpose

在第一个数据集中,每个员工都有一个团队负责人和一个主管。我可以转移那个没问题。

data a;
input employee_id ReportsTo $ ReportsToType $12.;
cards;
100 Jane  Supervisor
100 Mark  Team_lead
101 Max   Supervisor
101 Marie Team_lead
102 Sarah Supervisor
102 Sam   Team_lead
;
run;

proc transpose data = a
                out = aTP(drop =  _:);
by employee_id;
id ReportsToType;
var ReportsTo;
run; 
/* Output */
/*employee_id   Supervisor  Team_lead */
/*100                 Jane       Mark */
/*101                  Max      Marie */
/*102                Sarah        Sam */

现在,如果一名员工可以拥有1到3名团队领导者,该怎么办?

data b;
input employee_id ReportsTo $ ReportsToType $12.;
cards;
100 Jane  Supervisor
100 Mark  Team_lead
100 Jamie Team_lead  
101 Max   Supervisor
101 Marie Team_lead
101 Satyendra Team_lead
101 Usha      Team_lead
102 Sarah Supervisor
102 Sam   Team_lead
;
run;

/* Desired Output */
/*employee_id   Supervisor  Team_lead1     Team_lead2  Team_lead3 */
/*100                 Jane        Mark          Jamie             */
/*101                  Max       Marie      Satyendra        Usha */
/*102                Sarah         Sam                            */

使用proc transpose会出现错误,告诉我每组中不能有多个相同的ID变量。是否有转置程序允许这样做?

ERROR: The ID value "Team_lead" occurs twice in the same BY group

1 个答案:

答案 0 :(得分:1)

您需要更改输入数据,而不是重复单词Team_lead,它会显示递增...即Team_lead1Team_lead2等......

您可以使用by-group处理和retain语句来实现此目的:

proc sort data=b;
  by employee_id reportstotype;
run;

data want;
  set b;
  by employee_id reportstotype;
  retain cnt .;

  if first.reportstotype then do;
    cnt = 1;
  end;

  if upcase(reportsToType) eq 'TEAM_LEAD' then do;
    reportsToType = cats(reportsToType,cnt);
  end;

  cnt = cnt + 1;

run;

然后像以前一样调用proc transpose

proc transpose data=want out=trans;
  by employee_id;
  id reportsToType;
  var reportsTo;
run;