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

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

1 ACCEPTED SOLUTION

Accepted Solutions
HB
Barite | Level 11 HB
Barite | Level 11

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

3 REPLIES 3
Shmuel
Garnet | Level 18

Use function findw instead of find:

 

data want;
set have;
     if findw(upcase(label), 'NOS' , ' -');
run;
ybz12003
Rhodochrosite | Level 12

Hi Shmuel:

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

HB
Barite | Level 11 HB
Barite | Level 11

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 3 replies
  • 399 views
  • 2 likes
  • 3 in conversation