sas

Merge values of variables that are mixed: numeric and character


I have a dataset that looks like this:

dataset1;
input Variable1 Variable2;
cards;
124.3   1243
721     721
V304     .
332.9   3329
V202     .
;
run;

Is there a way to get the following?

dataset2;
input Variable1 Variable2;
cards;
124.3   1243
721     721
V304    V304
332.9   3329
V202    V202
;
run;

In other words I need to replace missing values in Variable2 with values in Variable1. I tried to use "coalesce" but it doesn't work maybe because values are both numeric and character. Overall the two variables (Variable1 and Variable2) are char.

Thank you very much.


Solution

  • Use coalescec to put them together. If variable2 is already missing then you can skip the if statement.

    data want;
        set dataset1;
        
        if(strip(variable2) = '.') then variable2 = '';
    
        variable2 = coalescec(variable2, variable1);
    run;