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

Trying to code data so I can count the number of times the phrase "Medicare" appears in the insurance type listed. I used the index only to see that it counts where in the phrase it occurs for example, Secondary Medicare, was counted as 11. Is there any way to eliminate the characters that appear before Medicare so I can create a variable that properly counts the number of patients with any type of Medicare insurance?

 

 

This was the original code I used:

data test;

set insurance;

Medicare = index (Insurancetype,'Medicare');

run;

 

 

data have:

 

Studyid     Insurancetype                                        Medicare

3548           Medicare                                                   1

6789            Managed Medicare                                  9

4567             Auto Generic/Secondary Medicare        25

 

data want:

Studyid     Insurancetype                                        Medicare

3548           Medicare                                                   1

6789            Managed Medicare                                  1

4567             Auto Generic/Secondary Medicare         1

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Try 

data test;
   set insurance;
   Medicare = (index (Insurancetype,'Medicare')>0);
run;

When SAS does a comparison such as >0 then a true result will return 1 and false 0.

 

Note that I pasted the code into a code box opened using the forum's {I} icon. The code box will maintain format of text from the program editor or Log window while the forum main message windows will reformat the text, sometimes quite drastically.

 

 

View solution in original post

3 REPLIES 3
ballardw
Super User

Try 

data test;
   set insurance;
   Medicare = (index (Insurancetype,'Medicare')>0);
run;

When SAS does a comparison such as >0 then a true result will return 1 and false 0.

 

Note that I pasted the code into a code box opened using the forum's {I} icon. The code box will maintain format of text from the program editor or Log window while the forum main message windows will reformat the text, sometimes quite drastically.

 

 

PeterClemmensen
Tourmaline | Level 20
data have;
input Studyid Insurancetype :$50.;
infile datalines dlm=',';
datalines;
3548,Medicare
6789,Managed Medicare
4567,Auto Generic/Secondary Medicare
;

data want(drop=i);
   set have;
   Medicare=0;
   do i=1 to countw(Insurancetype);;
      if upcase(scan(Insurancetype, i, ' /'))=upcase('Medicare') then Medicare+1;
   end;
run;
stancemcgraw
Obsidian | Level 7
That also works! Thanks!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 797 views
  • 0 likes
  • 3 in conversation