sas - 如何在 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
- “Id”是随机生成的患者标识符。
- “日期”和“时间”是事件的时间戳。
- “Assigned_pat_loc”是医院的当前患者位置,格式为“unit^room^bed”。EIAB 是急诊科的内部代码,大部分入院流程都是通过急诊科进行的。
- “Prior_pat_loc”是患者在当前位置之前的位置。
- “活动”是对事件的描述。它包括“入院”、“转入”、“转出”、“出院”和“死亡”等条目。
- 您会注意到很多重复记录,其中在出发单元和接收单元中都记录了相同的转移。您将能够通过查看时间戳来判断 - 它们对于重复记录是相同的。
以下是变量的详细信息。
- r_id 是您将为其他患者的 id 生成的变量的名称。
- 患者 1 在 8w^201(单元 8w 的 201 房间)有两次房间共享事件;他与患者 2 共用房间 7 小时(6 月 1 日凌晨 1 点至早上 8 点),与患者 3 共用房间 96 小时(6 月 1 日早上 9 点至 6 月 5 日早上 9 点)。
- 患者 2 也有两室共享事件。第一个是在 10E^45(10E 单元 45 室)的患者 4,持续 18 小时(5 月 31 日上午 7 点至 6 月 1 日凌晨 1 点);第二个是 8w^201 中患者 1 的 7 小时剧集。
- 患者 3 仅在 8w^201 房间与患者 1 同房一次,持续 96 小时。
- 患者 4 也只有一次房间共享事件,患者 2 在 10E^45 房间,持续 18 小时。
- 请注意,房间共享剧集列出了两次,每个患者一次。
请任何人指导我如何做到这一点?
解决方案
在处理可能出现意外重叠情况的范围时,您可以枚举范围并执行更简单的逻辑来查找共享时间/单位/房间。
例子:
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;
推荐阅读
- ruby-on-rails-5 - Rails 关联过滤器显示 ID 而不是名称
- javascript - 如何在数据库中包含引号的文本框中设置值?
- reactjs - React Native 属性只能在对象上定义
- c# - 如何在模态引导程序中从发布操作显示确认消息
- snort - 如何在 Snort 中创建内容规则
- oracle - 从 Oracle 并发获取时间戳加序列
- php - 一些关于 array_search 的 php 编码,结果出乎意料
- android - onLocationChanged 未在 android Q (android 10) 上调用
- android - 无需请求权限的 Android 读写 - Theta
- python - 当我向 watson 的 api 发送文本消息时,Watson 对话节点不会调用该操作