DATA Step, Macro, Functions and more

Select statement equivalent

Reply
Super Contributor
Posts: 647

Select statement equivalent

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;

Super Contributor
Posts: 1,636

Re: Select statement equivalent

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;

Frequent Contributor
Posts: 84

Select statement equivalent

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:

Details

SAS evaluates the expression in an IF-THEN statement to produce a result that is either nonzero, zero, or missing. A nonzero and nonmissing result causes the expression to be true; a result of zero or missing causes the expression to be false.

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.

Comparisons

  • Use a SELECT group rather than a series of IF-THEN statements when you have a long series of mutually exclusive conditions.

  • Use subsetting IF statements, without a THEN clause, to continue processing only those observations or records that meet the condition that is specified in the IF clause.

Valued Guide
Posts: 765

Re: Select statement equivalent

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;

Super User
Posts: 9,681

Re: Select statement equivalent

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

Valued Guide
Posts: 765

Re: Select statement equivalent

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.

Ask a Question
Discussion stats
  • 5 replies
  • 221 views
  • 0 likes
  • 5 in conversation