BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
smunigala
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Try

 

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

View solution in original post

5 REPLIES 5
Reeza
Super User

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.  

smunigala
Obsidian | Level 7

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;

ballardw
Super User

Try

 

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

smunigala
Obsidian | Level 7

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

ballardw
Super User

@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.

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 3436 views
  • 6 likes
  • 3 in conversation