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:
65 | 10 | 1 | 0 | 1 |
65 | 15 | 1 | 0 | 3 |
76 | 20 | 1 | 0 | 1 |
111 | 9 | 0 | 1 | 0 |
124 | 30 | 0 | 1 | 0 |
138 | 31 | 0 | 1 | 0 |
166 | 35 | 1 | 0 | 1 |
166 | 47 | 1 | 0 | 3 |
176 | 11 | 1 | 0 | 1 |
196 | 24 | 0 | 1 | 0 |
218 | 50 | 1 | 0 | 1 |
242 | 16 | 1 | 0 | 1 |
242 | 21 | 1 | 0 | 3 |
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:
65 | 10 | 1 | 0 | 1 |
65 | 15 | 1 | 0 | 2 |
76 | 20 | 1 | 0 | 1 |
111 | 9 | 0 | 1 | 0 |
124 | 30 | 0 | 1 | 0 |
138 | 31 | 0 | 1 | 0 |
166 | 35 | 1 | 0 | 1 |
166 | 47 | 1 | 0 | 2 |
176 | 11 | 1 | 0 | 1 |
196 | 24 | 0 | 1 | 0 |
218 | 50 | 1 | 0 | 1 |
242 | 16 | 1 | 0 | 1 |
242 | 21 | 1 | 0 | 2 |
Would appreciate any help or suggestions. Thanks!
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
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);
Make sure that your dataset does not already contain phi_visits. I suspect that is your problem.
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.