BookmarkSubscribeRSS Feed
natsd
Calcite | Level 5

I have a dataset with several records per person and each record contains two dates (start and stop).

data hosp;

INPUT id pos  fst_dt mmddyy11. lst_dt mmddyy11. drg;

datalines ;

001 2 01/01/2010 01/02/2010 001

001 3 01/02/2010 01/03/2010 001

001 2 01/03/2010 01/04/2010 002

001 2 02/05/2011 02/06/2011 .

;

run; 

Based on the flag (0/1) I need to assign a first start date to several observations (i.e. if person was in the hospital for 3 days and have three records, I need all three records have one admission date). The same goes for the stop date.

I tried lag function and it works for the stop date, but not for the start.

data hospa;

  set new;

  by id;

  adm_dt=.;

  dis_dt=.;

  startlag=lag(fst_dt);

  endlag=lag(lst_dt);

  if first.id then cnt=0;

  cnt+1;

  if cnt+1 and hosp=1 then adm_dt=startlag;

  else if cnt+1 and hosp=0 then dis_dt=endlag;

 

  proc print;

format  fst_dt lst_dt adm_dt dis_dt startlag endlag date9.;

  run;

ods rtf close;

This is what the output looks like

                                                                                                

Obs  id  pos     fst_dt     lst_dt  drg  cnt  hosp     adm_dt     dis_dt   startlag     endlag 

                                                                                                

  1    1   2   01JAN2010  02JAN2010   1    1     1           .          .          .          . 

  2    1   3   02JAN2010  03JAN2010   1    3     1   01JAN2010          .  01JAN2010  02JAN2010 

  3    1   2   03JAN2010  04JAN2010   2    4     1   02JAN2010          .  02JAN2010  03JAN2010 

  4    1   2   05FEB2011  06FEB2011   .    5     0           .  04JAN2010  03JAN2010  04JAN2010 

                                                                                                

Thank you in advance for your input.

1 REPLY 1
art297
Opal | Level 21

If you provide an example dataset, in the form of a datastep, it will be much easier to see what you have and for someone to offer the best solution.  As for inserting your program, copy it, paste it to notepad, re-copy it, and paste it here.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 976 views
  • 0 likes
  • 2 in conversation