Reputation: 315
I always find myself having to quickly refresh by-group processing when I'm using first dot and last dot variables with data, but today I saw something interesting.
Here's a sample dataset:
data DS1;
input ID1 ID2;
datalines;
1 100
1 200
1 300
2 400
3 500
3 500
4 600
;
run;
I generally use retain with by-group processing and either first or last dot variables to manipulate my data like so:
data ByGroup1;
set DS1;
by ID1 ID2;
retain Count;
if first.ID1 then Count = 0;
Count + 1;
run;
But, I was reading a post of SAS.com where an invidual used the following method (without a retain statement).
data ByGroup2;
set DS1;
by ID1 ID2;
if first.ID1 then Count = 0;
Count + 1;
run;
Both methods return the same dataset:
ID1 ID2 Count
1 100 1
1 200 2
1 300 3
2 400 1
3 500 1
3 500 2
4 600 1
Are variables implicitly retained in the PDV when doing by group processing? Or, is it that the program never returns to the top of the data-step until it reaches the end of the by-group?
I think I'm confused on the mechanics of how this variable is iterating without a retain since I'm so used to explicitly using retain when it's required.
Upvotes: 2
Views: 466
Reputation: 51581
When you use a sum statement the variable is automatically retained. And unless you have defined a different initial value it will be initialized to zero.
The syntax for a sum statement is:
variable + expression ;
Upvotes: 4