I have several insurance variables that are coded as 1 if someone chose those insurances in a survey (i.e., Medicare, Medicaid, COBRA etc.).
For example, Medicaid in a proc freq looks like this:
Medicaid Frequency Percent
1 59 100.0
I want to create a new variable that combines all my insurance variables. How do I go about doing this? I'm pretty stuck.
What do you want it to look like???
Is the result supposed to be numeric or character?
How many variables are there?
I think that you need to provide a few worked examples with at least a few variables. It may also help to describe how you intend to use that variable later for suggestions.
These are all character variables. I want to run a bivariate analysis on the association between insurance variables and PrEP usage without going through all the insurances individually. Basically, does have insurance influence PrEP usage? There are 11 insurance variables.
@westbestern wrote:
These are all character variables. I want to run a bivariate analysis on the association between insurance variables and PrEP usage without going through all the insurances individually. Basically, does have insurance influence PrEP usage? There are 11 insurance variables.
I would do something similar to
data want; set have; array ins(*) <list the names of your insurance variables here>; have_ins = (index(cats(of ins(*)),'1')>0); end;
The CATS function concatenates all the values of the variables in the array, the Index function returns the first position of the character 1, if present or 0 other wise. SAS will return a numeric 1 for that index value greater than 0 or 0 otherwise.
Suggestion for future stuff. If your variables are dichotomous, such as "has this characteristic/does not have" then coding them as 1/0 (have, don't have; true/false; yes/no) numeric variables is more flexible than character.
Max(of array(*)) would return 1 if any were 1(present/true whatever) or 0 if none were.
Sum(of array(*)) tells you how many had a 1
Mean(of array(*)) tells you the percent of 1 were present (.8 = 80%)
Min(of array(*)) =1 tells you all the values were 1
Range(of array(*)) =1 tells you at least one value was 0 and at least one was 1, a range of 0 tells you all the values were the same.
With character values a tad more coding would be needed for most of these, especially percentages.
Are these dichotomous fields? Also, are they mutually exclusive?
I made some fake data under these assumptions:
data have;
input pt medicaid medicare cobra private 3.;
datalines;
1 1 0 0 0
2 0 1 0 0
3 0 0 1 0
4 0 0 0 1
5 0 0 1 0
6 0 0 0 1
;
run;
data want;
set have;
array _ins [*] medicaid -- private;
do i = 1 to dim(_ins);
if _ins[i] = 1 then insurance = vname(_ins[i]);
end;
run;
Obs pt medicaid medicare cobra private i insurance 1 1 1 0 0 0 5 medicaid 2 2 0 1 0 0 5 medicare 3 3 0 0 1 0 5 cobra 4 4 0 0 0 1 5 private 5 5 0 0 1 0 5 cobra 6 6 0 0 0 1 5 private
It's always a good habit to post example data that shows us your exact problem. See here. If it contains PHI or PII, try to emulate it with fake data as I did above.
I think you are looking for a statement to add to a DATA step, such as:
have_insurance = (cobra="1") or (medicaid="1") or (medicare="1");
Extend the list to include all 11 types of insurance.
HAVE_INSURANCE will be numeric, with a value of 1 or 0. When any type of insurance types exists, it will be 1.
This assumes (as you stated) that all the insurance fields are character. If they are actually numeric, the quotes should be removed.
Hi @westbestern
Here's an nice solution demonstrated by 3 variables (you can extend to any number you like):
data a;
x1='0'; x2='0'; x3='0'; z = ifn(catt(of x1-x3)='000',0,1); output;
x1='1'; x2='0'; x3='0'; z = ifn(catt(of x1-x3)='000',0,1); output;
x1='1'; x2='1'; x3='0'; z = ifn(catt(of x1-x3)='000',0,1); output;
x1='0'; x2='0'; x3='1'; z = ifn(catt(of x1-x3)='000',0,1); output;
run;
Eyal
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.