SAS: how to bring values in the next couple of rows to the current row but don't know how far down to look

♀尐吖头ヾ 提交于 2020-01-06 05:17:25

问题


I have a data set that tracks the location change for a person. The data set includes person ID, a serial number for records within person ID, start datetime for the current location, leave datetime for this location, current location code and the prior location code.

My goal is to reorganize the data set so that I know on each row for a location how long the person actually stayed and the rows should still stay in the order ofdate/time ascending.

see a snippet of the data set for example below:

|   id|    rec_no|      enter_datetime|   leave_datetime|         loc| piror_loc|
---------------------------------------------------------------------------------
|    1|         1|   1/10/2009 6:27 pm|1/10/2009 6:29 pm|   SICU^6108|   64^6422|
|    1|         2|   1/10/2009 6:29 pm|1/13/2009 5:26 pm|   SICU^6108| SICU^6108|
|    1|         3|   1/13/2009 5:26 pm|1/16/2009 5:24 pm|     64^6440| SICU^6108|
|    1|         4|   1/16/2009 5:24 pm|1/16/2009 5:34 pm|   SICU^SICX|   64^6440|
...
...
|    1|         8|   2/25/2009 3:45 pm|2/25/2009 3:58 pm|     64^6418|   64^6438|
|    1|         9|   2/25/2009 3:58 pm|3/9/2009  3:16 pm|     64^6418|   64^6418|
|    1|        10|   3/9/2009  3:16 pm|3/9/2009  3:16 pm|     64^6418|   64^6418|

The first two rows show that this patient stayed at "SICU^6108" until 1/13/2019 5:26 pm. So these two rows should be combined into one.

The last 3 records show that the stay at "64^6418" lasted until 3/9/2009 3:16 pm. Hence these last three rows should be combined into one.

Row with rec_no = 3 and rec-no = 4 should stay as they are.

The end goal of the data set should be like:

|   id| rec_no|      enter_datetime|    leave_datetime|        loc| prior_loc|
-----------------------------------------------------------------------------
|    1|      1|  1/10/2009  6:27 pm|1/13/2009 5:26 om|   SICU^6108|   64^6422|
|    1|      3|   1/13/2009 5:26 pm|1/16/2009 5:24 pm|     64^6440| SICU^6108|
|    1|      4|   1/16/2009 5:24 pm|1/16/2009 5:34 pm|   SICU^SICX|   64^6440|
...
...
|    1|      8|  2/25/2009  3:45 PM| 3/9/2009 3:16 PM|     64^6418|   64^6438|

I am using SAS. I am thinking I should use lag/lead procedures/functions to get the date/time value of the next row (or rows) if the location stays the same. But the issue that I am having is you don't know how many records to go down to get the correct end (or start) datetime for a location. In the data set example provided, the first two rows you only look down/ahead 1 row. But the last three records, you need to look down 2 rows.


回答1:


What about sorting in reverse datetime order to achieve this?

proc sort data = have;
  by id descending enter_datetime;
run;

data want;
  set have;
  by id;

  retain final_leave_dt;

  if missing(final_leave_dt) then do;
    final_leave_dt = leave_datetime;
  end;

  if assigned_pat_loc_unit_nm ~=  prior_pat_loc_unit_nm then do;
    leave_datetime = final_leave_dt;
    output;
    call missing(final_leave_dt);
  end;
run;


来源:https://stackoverflow.com/questions/58531210/sas-how-to-bring-values-in-the-next-couple-of-rows-to-the-current-row-but-dont

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!