BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Lapis Lazuli | Level 10

Hi guys, 

suppose to have the following: 

 

data DB;
  input ID :$20. Admission :date09. Discharge :date09. Index;
  format Admission date9. Discharge date9.;
cards;
0001  13JAN2017 25JAN2017  0 
0001  13JAN2017 25JAN2017  1
0001  22FEB2018 03MAR2018  1   
0001  30JAN2019 04MAR2019  0  
0002  01DEC2018 14DEC2018  0 
0002  25DEC2018 02JAN2019  0 
0002  25NOV2020 03DEC2020  1 
0003  09JAN2016 25JAN2016  1 
0003  29JAN2018 12FEB2018  0 
...;

Is there a way to get the following? 

 

data DB1;
  input ID :$20. Admission :date09. Discharge :date09. Index;
  format Admission date9. Discharge date9.;
cards;
0001  13JAN2017 25JAN2017  0 
0001  13JAN2017 25JAN2017  1
0001  22FEB2018 03MAR2018  0   
0001  30JAN2019 04MAR2019  0  
0002  01DEC2018 14DEC2018  0 
0002  25DEC2018 02JAN2019  0 
0002  25NOV2020 03DEC2020  1 
0003  09JAN2016 25JAN2016  1 
0003  29JAN2018 12FEB2018  0 
...;

In other words let's consider ID = 0001. 

I would like: 

1) if Index = 1 for more than a date (adm-disch) for the same patient it will be 1 if it is the first date otherwise 0. This will regard only cases where Index = 1. For pts 0001 for example, for the same date (13JAN2017 25JAN2017) Index can be "0" or "1". If it is 0 nothing will happen and the change will regard the row where dates are 13JAN2017 - 25JAN2017. 

Thank you in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

First, please stop putting ...; in your cards. That throws an error and there is no need.

 

Basic retain a flag variable and act when set. The fun is in getting the timing for setting the flag.

 

data work.db1;
   set work.db;
   by id;
   retain isone;
   if first.id then isone=0;
   if index=1 then do;
      if isone then index=0;
      else isone=1;
   end;
   drop isone;
run;

View solution in original post

1 REPLY 1
ballardw
Super User

First, please stop putting ...; in your cards. That throws an error and there is no need.

 

Basic retain a flag variable and act when set. The fun is in getting the timing for setting the flag.

 

data work.db1;
   set work.db;
   by id;
   retain isone;
   if first.id then isone=0;
   if index=1 then do;
      if isone then index=0;
      else isone=1;
   end;
   drop isone;
run;