Search code examples
group-bysasdummy-variable

SAS: Coding a dummy variable for a value of a variable by group within group


I have a dataset of CASE_ID (x y and z), a set of multiple dates (including duplicate dates) for each CASE_ID, and a variable VAR. I would like to create a dummy variable DUMMYVAR by group within a group whereby if VAR="C" for CASE_ID x on some specific date, then DUMMYVAR=1 for all observations corresponding to CASE_ID x on with that date.

I believe that a Classic 2XDOW would be the key here but this is my third week using SAS and having difficulty getting this by two BY groups here.

I have referenced and attempted to write a variation of Haikuo's code here:

 PROC SORT have;
         by CASE_ID DATE;
    RUN;

    data want;
    do until (last.DATE);
      set HAVE;
       by date notsorted; 
       if var='c' then DUMMYVAR=1; 

    do until (last.DATE);
      set HAVE;
       by DATE notsorted;

       if DATE=1 then ????????

    end;
    run;

Solution

  • Change your BY statements to match the grouping you are doing. And in the second loop add a simple OUTPUT; statement. Then your new dataset will have all the rows in your original dataset and the new variable DUMMYVAR.

    data want;
      do until (last.DATE);
        set HAVE;
        by case_id date; 
        if var='c' then DUMMYVAR=1; 
      end;
      do until (last.DATE);
       set HAVE;
        by case_id date; 
        output;
      end;
    run;
    

    This will create the variable DUMMYVAR with values of either 1 or missing. If you want the values to be 1 or 0 then you could either set it to 0 before the first DO loop. Or add if first.date then dummyvar=0; statement before the existing IF statement.