lady8506
lady8506

Reputation: 25

Only one IF ELSE statement working SAS

Can someone explain to me why only the first IF ELSE statement in my code works? I am trying to combine multiple variables into one.

DATA BCMasterSet2;
SET BCMasterSet;
drop PositiveLymphNodes1; 
if PositiveLymphNodes1 = "." then PositiveLymphNodes =   
put(PositiveLymphNodes2, 2.);
else PositiveLymphNodes = PositiveLymphNodes1;
if PositiveLymphNodes2 = "." then new_posLymph = put(PositiveLymphNodes, 
2.);
else new_posLymph = PositiveLymphNodes2;
RUN;

Here is a nice screenshot of what the incorrect output looks like:OUTPUT

Thanks!

Upvotes: 1

Views: 164

Answers (1)

Joe
Joe

Reputation: 63434

Hard to say without seeing all of your data, but I have a suspicion: is positivelymphnodes1 character or numeric? Is it ever actually equal to "."?

If you are trying to say "if PositiveLymphNodes1 is missing", then you can say that this way:

if missing(positivelymphnodes1) then ...

You can also do the same thing using coalesce or coalescec (the latter is character, the former numeric, in its return value). It chooses the first nonmissing argument. - so if the first argument is missing, it chooses the second.

positiveLymphNodes = coalescec(PositiveLymphNodes1, put(positiveLymphNodes2,2.));
new_posLymph = coalescec(positiveLymphNodes2, put(positiveLymphNodes,2.));

I would be curious why you're using put only in one place and not the other - use it in both or neither, I would suggest.

Upvotes: 1

Related Questions