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
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.
Use function findw instead of find:
data want;
set have;
if findw(upcase(label), 'NOS' , ' -');
run;
Hi Shmuel:
Your code is very neat. However, it doesn't work in my actual data. Thanks for spending the time helping me.
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.