Count variable not properly counting

Reply
New User
Posts: 1

Count variable not properly counting

Hi,

I am trying to create two different count variables -- PHI_visits (ie. visits when insurance=1) and Medicaid_visits (i.e. visits when insurance=0) -- to count the number of unique patient visits (patcode) with each unique provider (ID) that are with a privately insured patients and Medicaid-covered patients, respectively.  However, I am noticing that the code I am using is creating two count variables that seems to not be counting properly, and I cannot seem to figure out why.

 

Here is my code:

Data HAmod;
Set IN.HA;
retain ID insurance patcode Medicaid_visits PHI_visits;
by ID;
if first.ID then do;
Medicaid_visits=0;
PHI_visits=0;
end;
if insurance=0 then Medicaid_visits+1;
else if insurance=1 then PHI_visits+1;
Run;

 

However, when I check the results using proc print, this produces a table as follows:

 

ID PATCODE insurance Medicaid_visits PHI_visits
6510101
6515103
7620101
1119010
12430010
13831010
16635101
16647103
17611101
19624010
21850101
24216101
24221103

 

It should instead look like below if PHI_visits and Medicaid_visits are counting correctly:

However, when I check the results using proc print, this produces a table as follows:

 

ID PATCODE insurance Medicaid_visits PHI_visits
6510101
6515102
7620101
1119010
12430010
13831010
16635101
16647102
17611101
19624010
21850101
24216101
24221102

 

Would appreciate any help or suggestions. Thanks!

Super User
Posts: 21,464

Re: Count variable not properly counting

I think the RETAIN statement is the issue. 

Why do you have ID, insurance and patcode in there? 99% of the time data that already exists should not be in the RETAIN STATEMENT

Super User
Posts: 5,963

Re: Count variable not properly counting

Check the incoming data set in.HA.  It probably already contains PHI_visits (and might contain Medicaid_visits as well).  If so, whichever variable(s) already exist should be dropped:

 

set in.HA (drop=PHI_visits Medicaid_visits);

Super User
Posts: 8,589

Re: Count variable not properly counting

Make sure that your dataset does not already contain phi_visits. I suspect that is your problem.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Ask a Question
Discussion stats
  • 3 replies
  • 169 views
  • 0 likes
  • 4 in conversation