BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
CathyVI
Pyrite | Level 9

Hi,

I have the following dataset and I want to create a new variable when sas finds any mention of the first word, it will output but am not getting the right outcome.

For example, 

 

data check1;
input gnn $ 1-50;
datalines;
ESOMEPRAZOLE
ESOMEPRAZOLE MAGNESIUM
ESOMEPRAZOLE/MAGNESIUM
ESOMEPRAZOLE_calcium
ESOMEPRAZOLE HCL

ESOMEPRAZOLE H ;
run;

 

data check2;
set check1;
if GNN= "ESOMEPRAZOLE: " then new_drug = anagesic;
keep gnn;
run;

I added a colon (:) to the drug name so that sas can find all mention but its not working.

This is what I want as output

ESOMEPRAZOLEAnalgesics
ESOMEPRAZOLE MAGNESIUMAnalgesics
ESOMEPRAZOLE/MAGNESIUMAnalgesics
ESOMEPRAZOLE_calciumAnalgesics
ESOMEPRAZOLE HCLAnalgesics
ESOMEPRAZOLE HAnalgesics
1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

Try the FIND function 😊:

data check1;
	input gnn $ 1-50;
	datalines;
ESOMEPRAZOLE
ESOMEPRAZOLE MAGNESIUM
NOT AN ANALGESIC
ESOMEPRAZOLE/MAGNESIUM
ESOMEPRAZOLE_calcium
ESOMEPRAZOLE HCL
ESOMEPRAZOLE H 
;

data check2;
	set check1;
	if find(GNN, "ESOMEPRAZOLE") then new_drug='Anagesic';
run;

 

 

Result:
Obs gnn new_drug
1 ESOMEPRAZOLE Anagesic
2 ESOMEPRAZOLE MAGNESIUM Anagesic
3 BAD DRUG  
4 ESOMEPRAZOLE/MAGNESIUM Anagesic
5 ESOMEPRAZOLE_calcium Anagesic
6 ESOMEPRAZOLE HCL Anagesic
7 ESOMEPRAZOLE H Anagesic
Check out my Jedi SAS Tricks for SAS Users

View solution in original post

3 REPLIES 3
ballardw
Super User

If your : addition was intended to match values that start with a specific word or string of characters then : would go on the equal sign:

 

if GNN=: "ESOMEPRAZOLE" then new_drug = anagesic;

If you include a space at the end of the string to match, as you did, then it would not match the value of ESOMEPRAZOLE/MAGNESIUM or ESOMEPRAZOLE_calcium due to the / or _ .

 

Also if there is any chance that your variable GNN might sometimes have a value that starts with Esomeprazole, note the different case, then it doesn't match. You could work around that by using the UPCASE function to match the value regardless of original case:

if upcase (GNN) =: "ESOMEPRAZOLE" then new_drug = anagesic;
SASJedi
SAS Super FREQ

Try the FIND function 😊:

data check1;
	input gnn $ 1-50;
	datalines;
ESOMEPRAZOLE
ESOMEPRAZOLE MAGNESIUM
NOT AN ANALGESIC
ESOMEPRAZOLE/MAGNESIUM
ESOMEPRAZOLE_calcium
ESOMEPRAZOLE HCL
ESOMEPRAZOLE H 
;

data check2;
	set check1;
	if find(GNN, "ESOMEPRAZOLE") then new_drug='Anagesic';
run;

 

 

Result:
Obs gnn new_drug
1 ESOMEPRAZOLE Anagesic
2 ESOMEPRAZOLE MAGNESIUM Anagesic
3 BAD DRUG  
4 ESOMEPRAZOLE/MAGNESIUM Anagesic
5 ESOMEPRAZOLE_calcium Anagesic
6 ESOMEPRAZOLE HCL Anagesic
7 ESOMEPRAZOLE H Anagesic
Check out my Jedi SAS Tricks for SAS Users
CathyVI
Pyrite | Level 9

Thank you both. I wish I could accept both solutions because they both provided me a better understand of how to address the problem.

@ballardw Thank you, you provide the solution and refined my initial thought process.

@SASJedi Thank you so much. I accepted your solution because when I looked into the entire data, I found some drug name with weird name. For example 0.9%ESOMEPRAZOLE or HCL ESOMEPRAZOLE. I was able to use the find function to identify these ones.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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