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.
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.
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.
Thanks very much @maguiremq. your solution works perfectly! Thanks
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.