BookmarkSubscribeRSS Feed
sas_Forum
Calcite | Level 5

What is the difference between select when and if ,else if in sas datastep can any one help

7 REPLIES 7
Ksharp
Super User

In my eyes, there is no difference between them.

But I would use select when you have lots of conditions to judge, because it is look elegant and succinct.

Ksharp

ballardw
Super User

I would say the major difference is that If , else if allows more use of compound comparisons that may miss all of the cases you need to consider.

As Ksharp says if I need to consider more than 2 or 3 values for a single variable Select is easier to understand and I believe is supposed to execute quicker as it branches differently instead of evaluating a whole series of If then else if ....

The OTHERWISE statement with SELECT also serves as a better catch-all than a last "else" because different nesting in a series may not be as obvious that none of the criteria had been met.

I think the most cases I have dealt with in a single select when is around 194. I am not sure if you could even nest if then else that deep.

AjayKant
Calcite | Level 5

Just the way you use both work in similar phase and no need to understand difference because it give you same result .

as per your comfort you can either choose if then else or select when statement ;

I tried it on same dataset and find out the difference is how much they are used memory .

Dataset of 15000 observation and 20 variables is used to analyse .

Codes  processed using IF

data if  ;

set raw ;

if Region='Central' then do ;

code=1;

end ;

if Region='East' then do ;

code=2;

end ;

if Region='Wast' then do ;

code=3;

end ;

else code=0 ;

run ;

*----------------------------------------*

NOTE: There were 15000 observations read from the data set WORK.RAW.

NOTE: The data set WORK.IF has 15000 observations and 18 variables.

NOTE: DATA statement used (Total process time):

other is using select when

data when ;

set raw ;

select (Region);

when(central)do ;

code=1 ;

end ;

when(East)do ;

code=2 ;

end ;

when(West)do ;

code=3 ;

end ;

otherwise code=0 ;

end ;

run ;

----------------------------------------------

NOTE: There were 15000 observations read from the data set WORK.RAW.

NOTE: The data set WORK.WHEN has 15000 observations and 21 variables.

NOTE: DATA statement used (Total process time):

Timing used for processing IF

      real time           0.03 seconds

      user cpu time       0.01 seconds

      system cpu time     0.01 seconds

      memory              362.75k

      OS Memory           6584.00k

      Timestamp           01/17/2014 07:48:49 PM

Timing used for processing select when ;

      real time           0.09 seconds

      user cpu time       0.01 seconds

      system cpu time     0.09 seconds

      memory              367.62k

      OS Memory           6584.00k

      Timestamp           01/17/2014 07:51:27 PM

You find a bit difference in real time and cpu time ..

here is start using the way if data will increase to millions which SAS system  actual have to work .

the difference can me measured easily .

I hope it will help you understand .

ballardw
Super User

Try the time difference coding

if Region='Central' then

Else if Region='West' then

else if Region='East' then

else

AjayKant
Calcite | Level 5

Can you explain what is time difference in content of this....

CTorres
Quartz | Level 8

You can simplify the select statement eliminating the do-end blocks innecesary in this case:

data when ;

set raw ;

select (Region);

when('Central') code=1 ;

when('East') code=2 ;

when('West') code=3 ;

otherwise code=0 ;

end ;

run ;

AjayKant
Calcite | Level 5

Thanks and its reduce the slight tie as well but yes in terms of performance if function works well .

The stats i get after re-edit the codes .

NOTE: There were 15000 observations read from the data set WORK.RAW.

NOTE: The data set WORK.IF has 15000 observations and 18 variables.

NOTE: DATA statement used (Total process time):

      real time           0.03 seconds

      user cpu time       0.00 seconds

      system cpu time     0.01 seconds

      memory              363.84k

      OS Memory           6584.00k

      Timestamp           01/17/2014 08:47:50 PM

NOTE: There were 15000 observations read from the data set WORK.RAW.

NOTE: The data set WORK.WHEN has 15000 observations and 21 variables.

NOTE: DATA statement used (Total process time):

      real time           0.12 seconds

      user cpu time       0.03 seconds

      system cpu time     0.09 seconds

      memory              365.62k

      OS Memory           6584.00k

      Timestamp           01/17/2014 08:47:50 PM

after re-editing the function works great but a major in there real time and  CPU time and this is the only difference I found .

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
  • 7 replies
  • 6802 views
  • 3 likes
  • 5 in conversation