Hi All,
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;
RUN;
you can use select statement, but I don't know which one is better.
data male female other;
set sasuser.admit;
select (sex);
when ('M') output male;
when ('F') output female;
otherwise output other;
end;
run;
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.
Note: For greater efficiency, construct your IF-THEN/ELSE statement with conditions of decreasing probability.
hi ... there is one big difference betwee if-then-else and select that I don't think has been mentioned ... given ...
data x;
input a @@;
datalines;
1 2 1 2 1 3
;
run;
data set Y is created and when A is not 1 or 2, B is missing
data y;
set x;
if a eq 1 then b='one';
else
if a eq 2 then b='two';
run;
data set Z is NOT created and there is an error in the LOG ...
data z;
set x;
select(a);
when (1) b='one';
when (2) b='two';
end;
run;
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;
Ksharp
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.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.