You need to add two pieces. Let SAS know when a new physician begins, and set the count variable at that point. It's actually not difficult:
proc sort data=IN.AggregateDoc_0514; by phycode; run; Data medicaidpts; Set IN.AggregateDoc_0514;
by phycode;
if first.phycode then count=1; keep phycode patcode count insurance; if insurance=0; by phycode; retain count; count+1;
Run;
So you were missing the BY statement, and the reference to reset (if first.phycode then ,...) came too late. If the first patient for a physician had insurance, your IF statement (if insurance=0) would delete the data for that patient, and never reach the statement that resets COUNT.
... View more