user12907213
user12907213

Reputation:

If-else then do in SAS

Hi I would like to know the steps of a multiple if then condition.

I would like to do the following:

data _ ;
set _ ;
if condition 1 is true then do; 
if sub condition 1 is true then _ ;
else if sub condition2 is true then _;
else if ... ;
end;
else if condition 2 is true then do; /* Is it right? */
if sub condition 1 is true then _ ;
else if sub condition2 is true then _;
else if ... ;
end;
run;

Could you please tell me which the right steps are? I should include else if or else do?

For example: condition 1 can take values 1 or 0. sub-conditions (I will call them as test1,test2, test3, ...) are other conditions. So I would have something like :

data _ ;
set _ ;
if condition1 = 1 then do;
if test1 = . then test3=test2; else test3=test1; 
else if test1 = 'My test' or test2= 'My test' then test3=test2 else test3=test2; 
end; 
else if condition1=0 then do;
if test1 = . then test3=test2; else test3=test1; 
else if test1 = 'My test' or test2= 'My test' then test3=test2 else test3=test2; 
end; 
else test3=test2;
run; 

A sample of data could be:

condition1   test1        test2
1             .            M
0             My test      .
1             Love        home
0             Home         .

what I would like to select is, based on condition1 values,

if condition1 is 1 and test1 is . then assign to test3 test2's value, otherwise test3=test1; and so on.

My expected output would be then:

condition1   test1        test2       test3
1             .            M           M
0             My test      .           My test
1             Love        home         Love
0             Home         .           Home

Upvotes: 1

Views: 6809

Answers (1)

Tom
Tom

Reputation: 51621

Not sure what you are asking, but perhaps this will help you.

You can think of the nested ifs as additional conditions. So if you had

if test1 then do;
  if test2 then statement1 ;
  else if test3 then statement2 ;
end;

You could re-write it as

if test1 and test2 then statement1 ;
else if test1 and test3 then statement2 ;

Upvotes: 1

Related Questions