sas

Uniform a variable value by other variables


suppose to have the following:

data have;
  input ID $ (Start End) (:date.) Place $;
  format start end date9.;
cards;
0001 13JAN2015 20JAN2015 .
0001 13JAN2015 20JAN2015 1
0001 13JAN2015 20JAN2015 .
0001 21JAN2015 31DEC2015 .
0001 01JAN2018 31DEC2018 .
0001 01JAN2019 31DEC2019 .
0002 01JAN2015 31DEC2015 1
0002 01JAN2015 31DEC2015 .
0002 01JAN2015 31DEC2015 1
;

Is there a way to get the following?


data have1;
  input ID $ (Start End) (:date.) Place $;
  format start end date9.;
cards;
0001 13JAN2015 20JAN2015 1
0001 13JAN2015 20JAN2015 1
0001 13JAN2015 20JAN2015 1
0001 21JAN2015 31DEC2015 .
0001 01JAN2018 31DEC2018 .
0001 01JAN2019 31DEC2019 .
0002 01JAN2015 31DEC2015 1
0002 01JAN2015 31DEC2015 1
0002 01JAN2015 31DEC2015 1
;

In other words I need to uniform "Place" variable value = 1 by ID and Start-End.

Thank you in advance


Solution

  • Assuming Place is numeric:

    data have;
      input ID $ (Start End) (:date.) Place;
      format start end date9.;
    cards;
    0001 13JAN2015 20JAN2015 .
    0001 13JAN2015 20JAN2015 1
    0001 13JAN2015 20JAN2015 .
    0001 21JAN2015 31DEC2015 .
    0001 01JAN2018 31DEC2018 .
    0001 01JAN2019 31DEC2019 .
    0002 01JAN2015 31DEC2015 1
    0002 01JAN2015 31DEC2015 .
    0002 01JAN2015 31DEC2015 1
    ;
    
    data want(drop = p);
       do until (last.End);
          set have;
          by ID Start End;
          if Place then p = 1;
       end;
    
       do until (last.End);
          set have;
          by ID Start End;
          Place = p;
          output;
       end;
    run;
    

    Result:

    ID    Start      End        Place
    0001  13JAN2015  20JAN2015  1
    0001  13JAN2015  20JAN2015  1
    0001  13JAN2015  20JAN2015  1
    0001  21JAN2015  31DEC2015  .
    0001  01JAN2018  31DEC2018  .
    0001  01JAN2019  31DEC2019  .
    0002  01JAN2015  31DEC2015  1
    0002  01JAN2015  31DEC2015  1
    0002  01JAN2015  31DEC2015  1