NewUsr_stat
NewUsr_stat

Reputation: 2571

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.

Upvotes: 0

Views: 29

Answers (1)

Stu Sztukowski
Stu Sztukowski

Reputation: 12849

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;

Upvotes: 2

Related Questions