IF-THEN vs IF in SAS
sas
Solution
An if-then statement conditionally executes code. If the condition is met for a given observation, whatever follows the 'then' before the `;` is executed, otherwise it isn't. In your example, since what follows is `output`, only observations with type `'H'` are output to the data set(s) being built by the data step. You can also have an if-then-do statement, such as in the following code:
if type = 'H' then do;
i=1;
output;
end;
If-then-do statements conditionally execute code between the `do;` and the `end;`. Thus the above code executes `i=1;` and `output;` only if type equals `'H'`.
An `if` without a `then` is a "subsetting if". According to SAS documentation:
A subsetting IF statement tests the condition after an observation is read into the Program Data Vector (PDV). If the condition is true, SAS continues processing the current observation. Otherwise, the observation is discarded, and processing continues with the next observation.
Thus if the condition of a subsetting if (ex. `type='H'`) is not met, the observation is not output to the data set being created by the data step. In your example, only observations where type is `'H'` will be output.
In summary, both of your example codes produce the same result, but by different means. `if type='H' then output;` only outputs observations where type is `'H'`, while `if type='H'; output;` discards observations where type is not `'H'`. Note that in the latter you don't need the `output;` because there is an implicit output in the SAS data step, which is only overridden if there is an explicit `output;` command.
Problem
What is the difference between `IF` and `IF-THEN` For example the following statement ``` if type='H' then output; vs if type='H'; output; ```