Help using Base SAS procedures

SAS code for Contain for multiple variables

Accepted Solution Solved
Reply
Contributor
Posts: 59
Accepted Solution

SAS code for Contain for multiple variables

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


Accepted Solutions
Solution
‎05-16-2017 11:10 AM
Super User
Posts: 11,343

Re: SAS code for Contain for multiple variables

Posted in reply to smunigala

Try

 

Ins1=  (find(catx('|',of PLAN_NAME1-PLAN_NAME6),  'MEDICARE')  > 0 );

View solution in original post


All Replies
Super User
Posts: 19,855

Re: SAS code for Contain for multiple variables

Posted in reply to smunigala

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.  

Contributor
Posts: 59

Re: SAS code for Contain for multiple variables

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;

Solution
‎05-16-2017 11:10 AM
Super User
Posts: 11,343

Re: SAS code for Contain for multiple variables

Posted in reply to smunigala

Try

 

Ins1=  (find(catx('|',of PLAN_NAME1-PLAN_NAME6),  'MEDICARE')  > 0 );

Contributor
Posts: 59

Re: SAS code for Contain for multiple variables

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) ;

Super User
Posts: 11,343

Re: SAS code for Contain for multiple variables

Posted in reply to smunigala

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.

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 235 views
  • 5 likes
  • 3 in conversation