I am trying to understand what this program does.
data a;
set b (keep=c d);
by c;
if first.c then e=0;
e+d;
if last.c;
run;
What confuses me is the following.
1), When there is not THEN statements, what does and IF statement do?
2), When there is no value evalueated, what does an IF statement do ? (why would it not say something like if first.c =1 ?)
3), My understanding is that e is an accumulator variable starting from 0, and d is added by iteration. But for some reason e represents the total for all values of c, not d even though we are adding d.
I am thinking that the first two questions will answer my question 3, but I would really appreciate your help.
In SAS there are two different if
statements - if-then-else
and subsetting if
. They have similar syntax but they do different things. You have one of each in your code.
This is how it's explained in the documentation:
The subsetting IF statement is equivalent to this IF-THEN statement:
if not (expression) then delete;
Without a THEN clause the IF statement is a subsetting statement. Basically if the condition is not true the current iteration of the data step stops and control returns to the top to process the next observation.
SAS evaluates any value that is not zero or missing as true. The automatic FIRST. and LAST. variables generated when you use a BY statement will have values of either 1 (true) or 0 (false). The FIRST. variable will be true when on the first observation for the BY group and the LAST. variable will be true when on the last observation for the BY group.
Your data step is getting the sum of the values of D for each value of C.