BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8

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;

5 REPLIES 5
Linlin
Lapis Lazuli | Level 10

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;

Resa
Pyrite | Level 9

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.

MikeZdeb
Rhodochrosite | Level 12

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;

Ksharp
Super User

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

MikeZdeb
Rhodochrosite | Level 12

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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 975 views
  • 0 likes
  • 5 in conversation