Searching text substring issue?

Accepted Solution Solved
Reply
Super Contributor
Posts: 335
Accepted Solution

Searching text substring issue?

Hello,

 

I have a problem when searching the text substring "NOS".   My codes is list below.  

 

data have;

length label $ 120;

input label &;

cards;

5-AMINOSALICYLIC ACID

ANTIEMESIS-NOS

GI/PROKINETIC AGENTS

FLINTSTONES MULTIVITAMINS- NOS

FRUCTUS CYNOSBATI, HERB

INOSITOL

INOSITOL NIACINATE

LACTOBACILLUS RHAMNOSUS

MIDOL NOS

GI/ANTIEMETICS

NOS- ANTACID

;

run;

 

data want;

set have;

Labelpos1=find(label,"NOS","i");

run;

 

data nos;

set want;

where Labelpos1 ne 0;

run;

 

The result I am looking for is:

ANTIEMESIS-NOS

FLINTSTONES MULTIVITAMINS- NOS

MIDOL NOS

NOS- ANTACID

 

However I found some other names containing "NOS" in the middle of the text, I don't want to include them.  The list is shown below.  Please advice how to fix it.  Thanks.

The list I don't want:

5-AMINOSALICYLIC ACID

FRUCTUS CYNOSBATI, HERB

INOSITOL

INOSITOL NIACINATE

LACTOBACILLUS RHAMNOSUS


Accepted Solutions
Solution
Friday
Regular Contributor
Regular Contributor
Posts: 185

Re: Searching text substring issue?

[ Edited ]

It looks like you want NOS in three conditions:

1.  Space before and after   NOS

2. Hyphen before    -NOS

3. Hyphen after     NOS-

 

So one option (there are probably cleaner options) would be to modify your existing code as follows:

 

data want;
set have;
Labelpos1=find(label," NOS ","i");
Labelpos2=find(label,"-NOS","i");
Labelpos3=find(label,"NOS-","i");
run;

data nos;
set want;
where Labelpos1 ne 0 or Labelpos2 ne 0 or Labelpos3 ne 0;
run;

  That yields

 

The SAS System      
       
label Labelpos1 Labelpos2 Labelpos3
ANTIEMESIS-NOS 0 11 0
FLINTSTONES MULTIVITAMINS- NOS 27 0 0
MIDOL NOS 6 0 0
NOS- ANTACID 0 0 1

 

which you can clean up.

 

 

EDIT:  See?  I told you there was cleaner.    @Shmuel 's solution is MUCH cleaner.  Use it.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,615

Re: Searching text substring issue?

Use function findw instead of find:

 

data want;
set have;
     if findw(upcase(label), 'NOS' , ' -');
run;
Super Contributor
Posts: 335

Re: Searching text substring issue?

Hi Shmuel:

Your code is very neat.  However, it doesn't work in my actual data.   Thanks for spending the time helping me.

Solution
Friday
Regular Contributor
Regular Contributor
Posts: 185

Re: Searching text substring issue?

[ Edited ]

It looks like you want NOS in three conditions:

1.  Space before and after   NOS

2. Hyphen before    -NOS

3. Hyphen after     NOS-

 

So one option (there are probably cleaner options) would be to modify your existing code as follows:

 

data want;
set have;
Labelpos1=find(label," NOS ","i");
Labelpos2=find(label,"-NOS","i");
Labelpos3=find(label,"NOS-","i");
run;

data nos;
set want;
where Labelpos1 ne 0 or Labelpos2 ne 0 or Labelpos3 ne 0;
run;

  That yields

 

The SAS System      
       
label Labelpos1 Labelpos2 Labelpos3
ANTIEMESIS-NOS 0 11 0
FLINTSTONES MULTIVITAMINS- NOS 27 0 0
MIDOL NOS 6 0 0
NOS- ANTACID 0 0 1

 

which you can clean up.

 

 

EDIT:  See?  I told you there was cleaner.    @Shmuel 's solution is MUCH cleaner.  Use it.

☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 115 views
  • 2 likes
  • 3 in conversation