DATA Step, Macro, Functions and more

More efficient -(Select group and IF )why ?

Reply
Occasional Contributor
Posts: 16

More efficient -(Select group and IF )why ?

Hi ,
I'm preparing for SAs certification Base exam . I have one question . Which will be more efficient between Select group and If statment. I coded this programs for practice using IF statment in one datasep and select group in another . both does same tasks but i would like to know which will be more efficeint for usage and why ? I know here I'm using test data so efficiney cant be known but still I'm curious to know which one will be better to use between select group and IF/then else statment and why ?


Program 1
DATA PATIENT;
INPUT ID $ SALARY DEPT $;
DATALINES;
1129 10000 A
1387 10000 B
2304 10000 C
4759 20000 E
5438 20000 F
6488 20000 G
9012 20000 H
;
DATA PATIENT1;
SET PATIENT;
LENGTH DEPT1 $6.;
IF DEPT IN ('A','B','C','D'') THEN DO DEPT1='ABCD';
ELSE IF DEPT IN ('F','G','H','I'')
THEN DEPT1='FGHI';
run;

DATA PATIENT2;
SET PATIENT;
LENGTH DEPT2 $6.;

SELECT (DEPT);
WHEN ('A','B','C','D') DEPT2='ABCDE';
WHEN ('F','G','H','I') DEPT2='FGHI';
OTHERWISE;
END;
Run;
Thanx
Manish
Occasional Contributor
Posts: 16

Re: More efficient -(Select group and IF )why ?

Hi i tried executing multiple statments using Do loops in select statament and i got the desired reuslts.

SELECT (DEPT);
WHEN ('A','B','C','D') DO; DEPT1='ABCDE'; DEPT2='LGH'; END;
WHEN ('F','G','H','I') DEPT1='FGHIJKL';
OTHERWISE;
END;

I believe the performance of If statments and select statments and if statments depends on the data. If Program encounters True data on the top itself ( top of If and select statment ) Performance will be good but Which one to choose Between them while coding and Why ?

Thanx
Manish
Super User
Posts: 5,257

Re: More efficient -(Select group and IF )why ?

I would say that IF/SELECT are quit equal when it comes to compare performance.
There are many other things that has higher impact on performance.
But if you want to gain the best performance, I believe in IF you should put the most common true condition first. I'm not sure if this applies to SELECT as well.
Generally, I'll favor SELECT if there are more than two possible outcomes, since it is more readable.

/Linus
Data never sleeps
Occasional Contributor
Posts: 16

Re: More efficient -(Select group and IF )why ?

Thanx..
I got it the performance depends on By putting most common condition first and this applies to both If/select statment . This works for Select statment also Thanx for clearing my doubt

Manish
Ask a Question
Discussion stats
  • 3 replies
  • 122 views
  • 0 likes
  • 2 in conversation