Hi all,
I need to extract terms MEDICARE, MCARE, MCAR, MC, MEDICAID and MCAID from a list of string/ chacater variables in my data set (PLAN1- PLAN5). I know that =: will extract the above, but it will do so only at the begining of the string but not if my terms are at the end or middle. My terms may be at the begining or in the middle of each string variable.
Attached is my code but I need a simpler and better code which can also pick up string from middle of the senetence.
Can anyone help me with SAS codes for extracting the above from list of 6 variables PLAN_NAME1 to PLAN_NAME6. I need to create new variable MEDICARE (Yes/ No) if these variables PLAN_NAME1 to PLAN_NAME6 contain any term like MEDICARE, MCARE, MCAR, MC.
Similarly I need to create a new variable MEDICAID (Yes/ No) if the variables PLAN_NAME1 to PLAN_NAME6 contain any term like MEDICAID or MCAID.
I also tried find option but it gave me some numerical variable and not exactly 1/0 option.
DATA Insur_all_new1 ;
Set Insur_all_new ;
Ins1= find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MEDICARE') ;
run;
DATA Insur_all_new1 ;
Set Insur_all_new ;
if PLAN_NAME1 =: 'MEDICARE' then Ins1 = 1;
if PLAN_NAME2 =: 'MEDICARE' then Ins2 = 1;
if PLAN_NAME3 =: 'MEDICARE' then Ins3 = 1;
if PLAN_NAME4 =: 'MEDICARE' then Ins4 = 1;
if PLAN_NAME5 =: 'MEDICARE' then Ins5 = 1;
if PLAN_NAME6 =: 'MEDICARE' then Ins6 = 1;
if PLAN_NAME1 =: 'MCARE' then Ins1 = 1;
if PLAN_NAME2 =: 'MCARE' then Ins2 = 1;
if PLAN_NAME3 =: 'MCARE' then Ins3 = 1;
if PLAN_NAME4 =: 'MCARE' then Ins4 = 1;
if PLAN_NAME5 =: 'MCARE' then Ins5 = 1;
if PLAN_NAME6 =: 'MCARE' then Ins6 = 1;
if PLAN_NAME1 =: 'MCAR' then Ins1 = 1;
if PLAN_NAME2 =: 'MCAR' then Ins2 = 1;
if PLAN_NAME3 =: 'MCAR' then Ins3 = 1;
if PLAN_NAME4 =: 'MCAR' then Ins4 = 1;
if PLAN_NAME5 =: 'MCAR' then Ins5 = 1;
if PLAN_NAME6 =: 'MCAR' then Ins6 = 1;
if PLAN_NAME1 =: 'MC' then Ins1 = 1;
if PLAN_NAME2 =: 'MC' then Ins2 = 1;
if PLAN_NAME3 =: 'MC' then Ins3 = 1;
if PLAN_NAME4 =: 'MC' then Ins4 = 1;
if PLAN_NAME5 =: 'MC' then Ins5 = 1;
if PLAN_NAME6 =: 'MC PLUS' then Ins6 = 1;
if PLAN_NAME1 =: 'MEDICAID' then Ins1 = 2;
if PLAN_NAME2 =: 'MEDICAID' then Ins2 = 2;
if PLAN_NAME3 =: 'MEDICAID' then Ins3 = 2;
if PLAN_NAME4 =: 'MEDICAID' then Ins4 = 2;
if PLAN_NAME5 =: 'MEDICAID' then Ins5 = 2;
if PLAN_NAME6 =: 'MEDICAID' then Ins6 = 2;
if Ins1 = 1 or Ins2 = 1 or Ins3 = 1 or Ins4 = 1 or Ins5 = 1 or Ins6 = 1 then Medicare = 1;
if Ins1 = 2 or Ins2 = 2 or Ins3 = 2 or Ins4 = 2 or Ins5 = 2 or Ins6 = 2 then Medicaid = 1;
run;
Thanks for your help in advance!
Sat
Try
Ins1= (find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MEDICARE') > 0 );
FIND or INDEX will work. You probably also want to use arrays rather than manually do each check.
Can you explain further how FIND didn't work.
When I used FIND, I h=got a new variable Ins1 values as 0s and 1s but also other numbers such as 6, 25, 33, etc for values actually should be 1s. (yes/ No = 1/0)
DATA Insur_all_new1 ;
Set Insur_all_new ;
Ins1= find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MEDICARE') ;
run;
Try
Ins1= (find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MEDICARE') > 0 );
Can I search multiple terms using single code? What if I want to search MEDICARE and MCARE?
Do I need to use the code twice?
Ins1= (find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MEDICARE') >0) ;
Ins1= (find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MCARE') >0) ;
@smunigala wrote:
Can I search multiple terms using single code? What if I want to search MEDICARE and MCARE?
Do I need to use the code twice?
Ins1= (find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MEDICARE') >0) ;
Ins1= (find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MCARE') >0) ;
May depend on just how many of these there are before it gets cumbersome:
Ins1= (find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MEDICARE') >0) or
( find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MCARE') >0) ;
And actually the ( ... >0) are not needed with two or more of these as the OR does a comparison for true and if any of the resulting numbers are non-zero then the overall value is true. So
Ins1= find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MEDICARE') or find(catx('|',of PLAN_NAME1-PLAN_NAME6), 'MCARE') ;
should work as well.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.