Jacob Ian
Jacob Ian

Reputation: 699

double if then in sas

I want to write a code like this:

 DATA TEST;
        SET original;
        IF hour = 30 AND minutes = . or 0 THEN new_minutes = 30;
        ELSE if hour = 60 AND minutes = . or 0 then new_minutes = 60;

RUN;

If I used IF hour = 30 and minutes = . OR minutes = 0 THEN new_minutes = 30; THEN even if the new value should be 60 it changes to 30. I there a way to get around this besides changing all 0 to . (or all . to 0)

Upvotes: 1

Views: 147

Answers (3)

Chris J
Chris J

Reputation: 7769

You could even write it without any if statements, providing you can accept new_minutes being zero if the criteria are not satisfied :

data want ;
  set have ;
  new_minutes = (hour in(30,60) and minutes in(.,0)) * hour ;
run ;

Upvotes: 1

Joe
Joe

Reputation: 63424

You could use coalesce for this; that returns the first nonmissing result.

if hour = 30 and coalesce(minutes,0) = 0 then minutes=30;

I would also suggest that you could write it this way to simplify the code:

if hour in (30,60) and coalesce(minutes,0) = 0 then minutes=hour;

And you could even generalize that first part potentially, depending on what are otherwise legal value for hour. If hour can't be over 24 then

if hour > 24 and coalesce(minutes,0) = 0 then minutes=hour;

Would be one possibility, as well as checking to see if hour is divisible by 15 or 10 or something like that.

Upvotes: 1

DomPazz
DomPazz

Reputation: 12465

It sounds like you need ( ).

IF hour = 30 AND (minutes = . or minutes = 0) THEN new_minutes = 30;

Or you could use the in statement

IF hour = 30 AND minutes in ( . , 0) THEN new_minutes = 30;

Upvotes: 2

Related Questions