如何计算第一个事件与SAS中的最后一个事件之间具有多个ID重复的时间?

发布于 2025-01-29 06:29:34 字数 847 浏览 2 评论 0原文

我有一个带有患者数据的数据集。我想计算患者首次拜访和最后一次去医院之间经过了多少天。我还需要一个虚拟变量(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤云独去闲 2025-02-05 06:29:34

您将需要保留一个新变量才能获得累积的日子。如果您使用sum语句,则会自动保留变量。如果使用dif()函数在当前日期和上一个日期之间获得几天的差异,则可以保留天数本身,而不是需要保留实际的第一个日期值。

data have;
  input ID  DATE :yymmdd.;
  format date yymmdd10.;
cards;
1 2014-04-21
1 2015-01-29
1 2021-04-14
2 2020-01-03
2 2021-07-04
;

data want;
  set have;
  by id date;
  first_visit=first.id;
  days + dif(date);
  if first.id then days=0;
run;

几张笔记。

第一个。变量已经被编码为1/0,因此只需使用分配语句将其值保存到永久变量中即可。

请勿有条件运行DIF()函数。这将跳过将当前日期添加到堆栈中以进行检索。这就是为什么您应该将dif()添加到几天之前,然后将几天添加到零,以便对ID的第一个观察结果。

结果:

                           first_
Obs    ID          DATE     visit    days

 1      1    2014-04-21       1         0
 2      1    2015-01-29       0       283
 3      1    2021-04-14       0      2550
 4      2    2020-01-03       1         0
 5      2    2021-07-04       0       548

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.

data have;
  input ID  DATE :yymmdd.;
  format date yymmdd10.;
cards;
1 2014-04-21
1 2015-01-29
1 2021-04-14
2 2020-01-03
2 2021-07-04
;

data want;
  set have;
  by id date;
  first_visit=first.id;
  days + dif(date);
  if first.id then days=0;
run;

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:

                           first_
Obs    ID          DATE     visit    days

 1      1    2014-04-21       1         0
 2      1    2015-01-29       0       283
 3      1    2021-04-14       0      2550
 4      2    2020-01-03       1         0
 5      2    2021-07-04       0       548
尤怨 2025-02-05 06:29:34
data WANT;
  set HAVE;
  by ID;
  first_visit = first.ID;
  retain firstDate;
  if first.ID then firstDate = Date;
  days = Date - firstDate;
  drop firstDate;
run;
data WANT;
  set HAVE;
  by ID;
  first_visit = first.ID;
  retain firstDate;
  if first.ID then firstDate = Date;
  days = Date - firstDate;
  drop firstDate;
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文