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
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.