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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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