首页 > 解决方案 > 如何在 SAS 中将表格数据转换为另一种表格格式

问题描述

我坚持使用 SAS 编程功能将数据表从一种格式转换为另一种格式。表结构如下:

id  Date        Time    assigned_pat_loc    prior_pat_loc       Activity
1   May/31/11   8:00    EIAB^EIAB^6                             Admission
1   May/31/11   9:00    8w^201               EIAB^EIAB^6      Transfer to 8w
1   Jun/8/11    15:00   8w^201                                  Discharge
2   May/31/11   5:00    EIAB^EIAB^4                             Admission 
2   May/31/11   7:00    10E^45               EIAB^EIAB^4    Transfer to 10E
2   Jun/1/11    1:00    8w^201                  10E^45      Transfer to 8w
2   Jun/1/11    8:00    8w^201                                 Discharge
3   May/31/11   9:00    EIAB^EIAB^2                            Admission
3   Jun/1/11    9:00    8w^201               EIAB^EIAB^2    Transfer to 8w
3   Jun/5/11    9:00    8w^201                                 Discharge
4   May/31/11   9:00    EIAB^EIAB^9                           Admission
4   May/31/11   7:00    10E^45               EIAB^EIAB^9    Transfer to 10E
4   Jun/1/11    8:00    10E^45                                     Death

1

我想将其转换为下表。 在此处输入图像描述

以下是变量的详细信息。

请任何人指导我如何做到这一点?

标签: sas

解决方案


在处理可能出现意外重叠情况的范围时,您可以枚举范围并执行更简单的逻辑来查找共享时间/单位/房间。

例子:

data have;

length id date time 8 loc ploc $20 activity $10;
input 
id  Date& date11.   Time time5.   loc    ploc       Activity;
format date date9. time time5.;

datetime = dhms (date,0,0,0) + time;

length unit room bed punit proom pbed $4;

unit = scan(loc,1,'^');
room = scan(loc,2,'^');
bed  = scan(loc,3,'^');

punit = scan(ploc,1,'^');
proom = scan(ploc,2,'^');
pbed  = scan(ploc,3,'^');

drop loc ploc;

datalines;
1   31-May-2011   8:00    EIAB^EIAB^6          .                Admission
1   31-May-2011   9:00    8w^201               EIAB^EIAB^6      Transfer to 8w
1    8-Jun-2011  15:00    8w^201               .                Discharge
2   31-May-2011   5:00    EIAB^EIAB^4          .                Admission 
2   31-May-2011   7:00    10E^45               EIAB^EIAB^4      Transfer to 10E
2    1-Jun-2011   1:00    8w^201               10E^45           Transfer to 8w
2    1-Jun-2011   8:00    8w^201               .                Discharge
3   31-May-2011   9:00    EIAB^EIAB^2          .                Admission
3    1-Jun-2011   9:00    8w^201               EIAB^EIAB^2      Transfer to 8w
3    5-Jun-2011   9:00    8w^201               .                Discharge
4   31-May-2011   9:00    EIAB^EIAB^9          .                Admission
4   31-May-2011   7:00    10E^45               EIAB^EIAB^9      Transfer to 10E
4    1-Jun-2011   8:00    10E^45               .                Death
;


* Fill in the ranges to get data by hour;

data hours(keep=id in_unit in_room at_dt);
  set have;
  by id;

  retain at_dt in_unit in_room;

  if first.id then do;
    at_dt = datetime;
    in_unit = unit;
    in_room = room;
  end;
  else do;
    do at_dt = at_dt to datetime-1 by dhms(0,1,0,0);
      output;
    end;
    in_unit = unit;
    in_room = room;
  end;

  format at_dt datetime16.;
run;

* prepare for transposition;

proc sort data=hours;
  by at_dt in_unit in_room id;
run;

* transpose to know which time/unit/room has multiple patients;

proc transpose data=hours out=roomies_by_hour(drop=_name_ where=(not missing(patid2))) prefix=patid;
  by at_dt in_unit in_room ;
  var id;
run;

* 'unfill' the individual hours to get ranges again;

data roomies;
  set roomies_by_hour;
  by in_unit in_room patid1 patid2;

  retain start_dt end_dt;
  format start_dt end_dt datetime16.;

  if first.patid2 then
    start_dt = at_dt;

  if last.patid2 then do;
    end_dt = at_dt;
    length_hrs = intck('hours', start_dt, end_dt);
    output;
  end;
run;

* stack data flipping perspective of who shared with who;

data roomies_mirrored;
  set 
    roomies                                         /* patid1 centric */
    roomies(rename=(patid1=patid2 patid2=patid1))   /* patid2 centric */
  ;
run;

proc sort data=roomies_mirrored;
  by patid1 start_dt;
run;

推荐阅读