What is the difference between select when and if ,else if in sas datastep can any one help
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
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.
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 .
Try the time difference coding
if Region='Central' then
Else if Region='West' then
else if Region='East' then
else
Can you explain what is time difference in content of this....
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 ;
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 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.