Search code examples
sassas-macro

SAS Create conditional duplicate rows - repeating rows for an instance and assigning weight to the duplicate rows


I need to transform my data to input into a model. I am doing this with sas. Below is the original format of the data and two options of what the model will accept. Option 2 is ideal. Is there a way to do this in SAS? I keep trying to come up with data steps but end up in circles.

ORIGINAL DATA FORMAT

ID Total Risk
recordA 3 3
recordB 5 2

OPTION #1:

ID Target
recordA 1
recordA 1
recordA 1
recordB 1
recordB 1
recordB 0
recordB 0
recordB 0

OPTION #2:

ID Target Weight
recordA 1 3
recordB 1 2
recordB 0 3

I tried subtracting columns and making a flag for whether Risk>0 then Target 1 else 0 but run into issues creating repeated records


Solution

  • You can do this with a data step with two output statements. weight is always equal to risk when target = 1, and we always want to have an output row for that. If total does not equal weight, then we need to create a second output row where target = 0. In that case, weight is total - risk.

    data want;
        set have;
        
        target = 1;
        weight = risk;
        output;
    
        if(total NE risk) then do;
            target = 0;
            weight = total - risk;
            output;
        end;
    
        keep id target weight;
    run;
    

    Output:

    id        target   weight
    recordA   1        3
    recordB   1        2
    recordB   0        3