11-11-2011 09:43 AM
What is the equivalent select statement for the following if-then/else statements?And which one of the two is more efficient?
well, the conditional logic is not applied yet.hence there is no condition applied after the if statement.
DATA IPMD IPMDIS IPSD IPSDIS NONAD NONADIS;
SET DISK2 DISK3;
IF THEN OUTPUT IPMD;
ELSE IF THEN OUTPUT IPMDIS;
ELSE IF THEN OUTPUT IPSD;
ELSE IF THEN OUTPUT IPSDIS;
ELSE IF THEN OUTPUT NONAD;
ELSE THEN OUTPUT NONADIS;
11-11-2011 10:04 AM
you can use select statement, but I don't know which one is better.
data male female other;
when ('M') output male;
when ('F') output female;
otherwise output other;
11-17-2011 05:45 PM
Not sure if it answers your question on efficiency (AFAIK there is very little difference between the two) but the SAS documentation on the IF-THEN/ELSE statement (see here) states:
If the conditions that are specified in the IF clause are met, the IF-THEN statement executes a SAS statement for observations that are read from a SAS data set, for records in an external file, or for computed values. An optional ELSE statement gives an alternative action if the THEN clause is not executed. The ELSE statement, if used, must immediately follow the IF-THEN statement.
Using IF-THEN statements without the ELSE statement causes SAS to evaluate all IF-THEN statements. Using IF-THEN statements with the ELSE statement causes SAS to execute IF-THEN statements until it encounters the first true statement. Subsequent IF-THEN statements are not evaluated.
11-20-2011 10:31 PM
hi ... there is one big difference betwee if-then-else and select that I don't think has been mentioned ... given ...
input a @@;
1 2 1 2 1 3
data set Y is created and when A is not 1 or 2, B is missing
if a eq 1 then b='one';
if a eq 2 then b='two';
data set Z is NOT created and there is an error in the LOG ...
when (1) b='one';
when (2) b='two';
11-20-2011 10:58 PM
Mike . You lost a very important statement in SELECT.
data x; input a @@; datalines; 1 2 1 2 1 3 ; run; data z; set x; select(a); when (1) b='one'; when (2) b='two'; otherwise; end; run;
11-23-2011 11:46 AM
Hi ... that was the point of the posting, to show how SELECT can make a data step fail if one does not take into account all possibilities. SELECT forces you to pay attention to details while IF allows you to be sloppy.