Zach
Zach

Reputation: 45

Is This the Correct Way to Properly Reset a Counter in SAS?

I question if the my original code was not working properly, or not. Does inserting the 2nd line here solve any issues of data improperly affecting my runs?

data Count;
Count = 0;
set ptemp.HIC_Matched_2018dos_codes;
BY MemberKey;
if FIRST.MemberKey then
  Count = 0;
Count + 1;
if LAST.MemberKey;
run;

Upvotes: 0

Views: 247

Answers (1)

Tom
Tom

Reputation: 51566

You do not want that second line. It will reset the value of COUNT to 0 on every observation. So the result will be that COUNT will always be 1 no matter how many observations there are for each MemberKey value.

Without that line your code is counting the number of observations per MemberKey.

The other thing that is needed is also that COUNT is not already on the input dataset ptemp.hic_match_2018dos_codes. Because if it is then when the SET statement runs the retained count from the previous observation will be overwritten by the value read from the dataset.

Upvotes: 1

Related Questions