SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 882 views
  • 1 like
  • 2 in conversation