sas

Flag repeated dates by ID


suppose to have the following:

data DB;
  input ID :$20. Admission :date09. Discharge :date09.; 
  format Admission date9. Discharge date9.;
cards;
0001 13JAN2017 25JAN2017 
0001 13JAN2017 25JAN2017 
0001 13JAN2017 25JAN2017
0001 11MAR2017 16MAY2017 
0001 30JAN2019 04MAR2019 
0002 11SEP2014 15SEP2014 
0002 28DEC2014 03JAN2015 
0002 28DEC2014 03JAN2015 
;


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 1
0001 13JAN2017 25JAN2017 .
0001 13JAN2017 25JAN2017 .
0001 11MAR2017 16MAY2017 1
0001 30JAN2019 04MAR2019 1
0002 11SEP2014 15SEP2014 1
0002 28DEC2014 03JAN2015 1
0002 28DEC2014 03JAN2015 .
;

In other words, I would like to add an Index column containing a missing value to all the repeated dates except the first, by ID.


Solution

  • Just use BY group processing.

    data want;
      set db1;
      by id admission;
      index= first.admission;
    run;
    

    If you really want INDEX to be MISSING instead of FALSE then work a little harder.

    data want;
      set db1;
      by id admission;
      if first.adminssion then index=1;
    run;