SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
rykwong
Quartz | Level 8

Dear SAS community

I used this code to search a text field (procedure_name) but it yielded wrong data in all of the "pro_" fields.   But the if I use .... where procedure_name contains xxx then the results make sense so I know this field has searchable text.

Could you please tell me what is wrong with my code using index function?

 

data procedure1; set procedure;
pro_icd=0; pro_pacer=0; pro_emb=0; pro_cabg=0; pro_pci=0; pro_valvesurg=0; pro_codeblue=0; pro_ett=0; pro_ablation=0; pro_cor_angio=0; pro_ccta=0; pro_ccs=0;
if index(lowcase(procedure_name), "defibrillator")=1 then pro_icd=1;
if index(lowcase(procedure_name), "pacemaker")=1 then pro_pacer=1;
if index(lowcase(procedure_name), "biopsy of heart")=1 then pro_emb=1;
if indexc(lowcase(procedure_name), "bypass coronary artery","cardiac bypass graft surgery","coronary artery bypass")=1 then pro_cabg=1;
if index(lowcase(procedure_name), "dilation of coronary artery")=1 then pro_pci=1;
if index(lowcase(procedure_name), "cardiac valve replacement")=1 then pro_valvesurg=1;
if index(lowcase(procedure_name), "cardiopulmonary resusitation")=1 then pro_codeblue=1;
if index(lowcase(procedure_name), "cardiovascular stress test")=1 then pro_ett=1;
if index(lowcase(procedure_name), "ablation")=1 and index(lowcase(procedure_name), "heart")=1 then pro_ablation=1;
if indexc(lowcase(procedure_name), "coronary angiography","coronary arteriography")=1 then pro_cor_angio=1;
if index(lowcase(procedure_name), "heart")=1 and indexc(lowcase(procedure_name), "catheterization")=1 then pro_cor_angio=1;
if indexc(lowcase(procedure_name), "computed tomographic angiography, heart, coronary arteries")=1 then pro_ccta=1;
if index(lowcase(procedure_name), "coronary calcium")=1 then pro_ccs=1;
keep empi procedure_name code pro_test:;
run;

 

 

Thanks so much in advance

1 ACCEPTED SOLUTION

Accepted Solutions
A_Kh
Barite | Level 11

Hi 
Do you have procedure_name data to show here? There might be multiple reasons the function is not giving the expected results. 
Your current code assumes that the searched word comes first in the string in procedure_name variable without preceding spaces. 
If this assumption is true then try adding STRIP() function to your existing code. 

if index(lowcase(strip(procedure_name)), "defibrillator")=1 then pro_icd=1;


If the string doesn't start with the searched word you can use FIND function instead. It gives you 1 if true, and the condition works.  

if find(lowcase(procedure_name), "defibrillator")=1 then pro_icd=1;

 

View solution in original post

3 REPLIES 3
A_Kh
Barite | Level 11

Hi 
Do you have procedure_name data to show here? There might be multiple reasons the function is not giving the expected results. 
Your current code assumes that the searched word comes first in the string in procedure_name variable without preceding spaces. 
If this assumption is true then try adding STRIP() function to your existing code. 

if index(lowcase(strip(procedure_name)), "defibrillator")=1 then pro_icd=1;


If the string doesn't start with the searched word you can use FIND function instead. It gives you 1 if true, and the condition works.  

if find(lowcase(procedure_name), "defibrillator")=1 then pro_icd=1;

 

rykwong
Quartz | Level 8

this works!  thanks so much

I should not have assume =1 because the text did not always start on the first position.

very much appreciated

ballardw
Super User

Index returns the position number if the text is found. So if the target text might not start on the first column of the searched text:

if index(lowcase(procedure_name), "defibrillator") ge 1 then pro_icd=1;

ge is greater than or equal.

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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