0
votes

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
1
Not sure I understand what the question is. The DO/END block is just so you can replace a single statement with multiple statements. - Tom
In your example is the variable TEST1 numeric or character? In some places you are treating it as numeric, test1 = ., and in other places as character, test1 = 'My test'. You can use the missing() function to test if a variable is missing and it will work the same for numeric or character variables, plus it will detect special missing values like .A, .Z etc. - Tom
In a dataset I have found . and NA values (the latter as string). Is there any way to check the type of the variable? - user12907213
PROC CONTENTS will show you information about a dataset. - Tom

1 Answers

1
votes

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 ;