BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Emma_at_SAS
Lapis Lazuli | Level 10

I have a variable that contains details on how much medicine was administered for each patient during a stay at the hospital and I need to clean some observations.

Examples of clean observations are: 2 GRAMS, 20 OUNCE

for these clean examples, I create two variables one for the quantity and one for the unit. 

But some observations are: 4 GRAMS ONLY TODAY, 15 OUNCE ONLY TODAY.

For these examples, I need to remove ONLY TODAY, and then I can separate the quantity and unit values using COMPRESS

quantity=compress(DrugQuantity,"1234567890./","k");
unit=compress(DrugQuantity,"ABCDEFGHIJKLMNOPQRSTUVWXYZ ","k");

How can I remove all those ONLY TODAY parts from my original variable or from my compressed unit variable? I appreciate your suggestions.

 

1 ACCEPTED SOLUTION

Accepted Solutions
maguiremq
SAS Super FREQ

I think this calls for PRXCHANGE. If someone notices an issue with my regex, please feel free to correct it.

 

Errors may pop up depending on whether the words are consistently spelled the way you showed them.

 

data have;
var = "4 GRAMS ONLY TODAY, 15 OUNCE ONLY TODAY";
run;

data want;
	set have;
		want_var = prxchange("s/\s*?(ONLY TODAY)\s*?//", -1, var);
run;
Obs var want_var 
1 4 GRAMS ONLY TODAY, 15 OUNCE ONLY TODAY 4 GRAMS, 15 OUNCE 

The

\s*?

checks for zero or more spaces before or after the phrase ONLY TODAY.

View solution in original post

2 REPLIES 2
maguiremq
SAS Super FREQ

I think this calls for PRXCHANGE. If someone notices an issue with my regex, please feel free to correct it.

 

Errors may pop up depending on whether the words are consistently spelled the way you showed them.

 

data have;
var = "4 GRAMS ONLY TODAY, 15 OUNCE ONLY TODAY";
run;

data want;
	set have;
		want_var = prxchange("s/\s*?(ONLY TODAY)\s*?//", -1, var);
run;
Obs var want_var 
1 4 GRAMS ONLY TODAY, 15 OUNCE ONLY TODAY 4 GRAMS, 15 OUNCE 

The

\s*?

checks for zero or more spaces before or after the phrase ONLY TODAY.

Emma_at_SAS
Lapis Lazuli | Level 10

Thanks very much @maguiremq. your solution works perfectly! Thanks 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 2 replies
  • 613 views
  • 1 like
  • 2 in conversation