有这样一个类似的数据集,id代表患者编号,n代表随访次数,treat是用药方案,v_date 是随访日期。
怎么统计每个人用每种方案的分别的使用时间呢?
比如第一个人用a方案的时间应该为第3次减去第1次的时间,即(2012-5-3) -(2012-1-1)谢谢!
three是原始数据集。
data three;
input id n treat$ v_date : yymmdd10.;
format v_date yymmdd10.;
cards;
1 1 a 2012-1-1
1 2 a 2012-3-1
1 3 c 2012-5-3
1 4 c 2012-7-28
1 5 a 2012-10-1
1 6 a 2013-1-3
2 1 a 2009-3-9
2 2 a 2009-7-3
2 3 c 2009-12-1
2 4 a 2010-2-3
2 5 c 2010-5-4
2 6 c 2010-8-26
2 7 c 2010-12-3
;
run;
想得到每个人的这些观测
1 | 1 | a | 2012-1-1 |
1 | 3 | c | 2012-5-3 |
1 | 5 | a | 2012-10-1 |
1 | 6 | a | 2013-1-3 |
2 | 1 | a | 2009-3-9 |
2 | 3 | c | 2009-12-1 |
2 | 4 | a | 2010-2-3 |
2 | 5 | c | 2010-5-4 |
2 | 7 | c | 2010-12-3 |
或者更好的就是产生新变量,每个人只有一条记录,如:
| id | n | treat | v_date | treat_1 | v_date_1 | treat_2 | v_date_2 | treat_3 | v_date_3 | treat_4 | v_date_4 | treat_5 | v_date_5 | treat_6 | v_date_6 |
1 | 1 | a | 2012-1-1 | a | 2012-1-1 | c | 2012-5-3 | a | 2012-10-1 | a | 2013-1-3 | | | | |
2 | 1 | a | 2009-3-9 | | | c | 2009-12-1 | a | 2010-2-3 | c | 2010-5-4 | c | 2010-12-3 | | |
非常感谢!