如何计算第一个事件与SAS中的最后一个事件之间具有多个ID重复的时间?
我有一个带有患者数据的数据集。我想计算患者首次拜访和最后一次去医院之间经过了多少天。我还需要一个虚拟变量(0,1),如果观察到数据集中的患者(根据访问日期),则采用该值1。数据集看起来像:
Patient ID Visit date
1 2014-04-21
1 2015-01-29
1 2021-04-14
2 2020-01-03
2 2021-07-04
. .
. .
我想要的:
Patient ID Visit date First visit Difference between first visit and last (in days)
1 2014-04-21 1 0
1 2015-01-29 0 283
1 2021-04-14 0 2550
2 2020-01-03 1 0
2 2021-07-04 0 548
. . . .
. . . .
如果我按患者ID对数据集进行排序,然后访问日期并运行代码:如果First.PatientID,则进行; first_visit = 1; end;
我能够创建我的虚拟变量。我在计算第一次访问和上次访问之间的天数方面遇到了麻烦。非常感谢任何帮助。谢谢!
I have a dataset with patient data. I want to calculate how many days have passed between a patients first visit and their last to the hospital. I also need a dummy variable (0,1) that takes the value 1 if the observation was the patients first in the dataset (according to the visit date). The dataset looks like:
Patient ID Visit date
1 2014-04-21
1 2015-01-29
1 2021-04-14
2 2020-01-03
2 2021-07-04
. .
. .
What I want:
Patient ID Visit date First visit Difference between first visit and last (in days)
1 2014-04-21 1 0
1 2015-01-29 0 283
1 2021-04-14 0 2550
2 2020-01-03 1 0
2 2021-07-04 0 548
. . . .
. . . .
If I sort the dataset by Patient ID and Visit date and the run the code: if first.PatientID then do; First_visit = 1; end;
Im able to create my dummy variable. I have trouble with calculating the difference in days between first and last visit. Would greatly appreciate any help. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将需要保留一个新变量才能获得累积的日子。如果您使用sum语句,则会自动保留变量。如果使用dif()函数在当前日期和上一个日期之间获得几天的差异,则可以保留天数本身,而不是需要保留实际的第一个日期值。
几张笔记。
第一个。变量已经被编码为1/0,因此只需使用分配语句将其值保存到永久变量中即可。
请勿有条件运行DIF()函数。这将跳过将当前日期添加到堆栈中以进行检索。这就是为什么您应该将dif()添加到几天之前,然后将几天添加到零,以便对ID的第一个观察结果。
结果:
You will need to RETAIN a new variable to get the cumulative days. If you use a SUM statement then variable is automatically retained. If you use the DIF() function to get the difference in days between the current and previous date then you can just retain the DAYS variable itself rather than needing to retain the actual first date value.
A couple of notes.
The FIRST. variable is already coded 1/0 so just use an assignment statement to save its value into a permanent variable.
Do not run the DIF() function conditionally. That will skip adding the current date into the stack to be retrieved the next time. That is why you should add the DIF() to DAYS before forcing DAYS to zero for the first observations for an ID.
Results: