Help using Base SAS procedures

Help with assigning same date to several observations

Occasional Contributor
Posts: 9

Help with assigning same date to several observations

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 .



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;





  if then cnt=0;


  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.;


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.

Posts: 8,163

Re: Help with assigning same date to several observations

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.

Ask a Question
Discussion stats
  • 1 reply
  • 2 in conversation