In the coming days, I’ll be presenting at two user groups, one in eastern Canada in Halifax, and the other all the way south west in San Antonio, Texas. I’m really praying for great weather. I’ll carry an umbrella if I have to, and wear a scarf if I have to. But I don’t’ really want to, at least not in San Antonio in Texas.
Regardless of what the weather turns out to be, at least I can use the weather forecast to provide the data I need to explain IF and IF-THEN-ELSE statements to my Programming 2 students. This was their question:
Tell me the difference between the Subsetting IF and the IF—THEN—ELSE—IF statement?
Yes, at first glance they do appear similar.
However the Subsetting IF is a filter. If you are familiar with the WHERE clause, its role is similar to the WHERE, it filters or subsets rows based on a condition. It’s a bit different from the WHERE because it has access to even new variables that you are constructing in the data step.
The IF—THEN—ELSE statement is familiar to most programmers, regardless of their background. It performs an action based on a condition, but its role is not to subset data.
To really see the difference here’s some code I wrote up:
1 - First, I construct my weather dataset by pulling in data from a Google search
data weather; infile datalines dlm=','; input city : $12. day $ date: date9. high low weather :$9.; datalines; Toronto,Thu,3-Nov-16,12,3,rain Halifax,Thu,3-Nov-16,11,7,cloudy Halifax,Fri,4-Nov-16,10,4,rain San Antonio,Sat,5-Nov-16,25,14,sun cloud San Antonio,Sun,6-Nov-16,23,16,rain San Antonio,Mon,7-Nov-16,24,18,rain San Antonio,Tue,8-Nov-16,24,17,rain Toronto,Tue,8-Nov-16,13,7,rain ; run; NOTE: The data set WORK.WEATHER has 8 observations and 6 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00
2 - Next, I'll do some conditional processing. Notice my log note, how all rows are written to the output dataset:
data what; set weather; if city ='Halifax' and weather='rain' then whattotake="Sigh! I knew it, I'll take an umbrella"; else if city ='San Antonio' and weather='rain' then whattotake="No way! ok I'll take an umbrella"; flag='SAS user group conferences 3-8 november 2016'; run; NOTE: There were 8 observations read from the data set WEATHER. NOTE: The data set WORK.WHAT has 8 observations and 8 variables. NOTE: DATA statement used (Total process time): real time 0.01 seconds cpu time 0.00 seconds
3 - Finally, I’ll use a Subsetting If.
If the condition is true, only then the next action takes place, (i.e. flag is set to 'SAS etc.’), same as in the conditional processing scenario.
But the key driving difference is this. Only if the subsetting IF condition is true, (i.e. only if city is either Halifax or San Antonio) will SAS hit the RUN statement.
Implicit in the RUN statement are 2 actions, IMPLICIT output, and IMPLICIT return.
data halifaxSanAnt; set weather; if city in ('Halifax', 'San Antonio'); flag='SAS user group conferences 3-8 november 2016'; run; NOTE: There were 8 observations read from the data set WEATHER. NOTE: The data set WORK.HALIFAXSANANT has 6 observations and 7 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
There you go, the big differentiator between the IF..THEN..ELSE and Subsetting IF statements.
What do you think? Will it rain or will it shine?
Hope you enjoyed this blog post. I can’t wait to share more SAS tips at the two user groups in Halifax and San Antonio. If you are in the neighbourhood, stop by. And wish me fair weather.