data have;
input $ drug;
cards;
25 mg
12 mg
13 mg;
Run;
I need to create a new data that convert the chracter variable drug to numeric including only the numeric portion of the variable
output
drug
15
12
13
Thanks
data have;
infile datalines dlm=",";
input drug $;
want=input(compress(drug,,'kd'),best.);
datalines;
25 mg
12 mg
13 mg
;
run;
The compress 'kd' option means keep digits.
data have;
infile datalines dlm=",";
input drug $;
want=input(compress(drug,,'kd'),best.);
datalines;
25 mg
12 mg
13 mg
;
run;
The compress 'kd' option means keep digits.
Thank you RW9 for the prompt response!
Make sure to adapt the suggested code if your data contain non-integer values such as "12.5 mg". Otherwise the numeric dose will be wrong (like 125 in my example) because the decimal point will be deleted.
You could, for example, list all characters to be deleted in the second argument of the COMPRESS function:
want=input(compress(drug,'gmµu'),best.);
This would correctly transform values including a decimal point and still remove units "mg", "g", "ug" or "µg".
@FreelanceReinh wrote:
Make sure to adapt the suggested code if your data contain non-integer values such as "12.5 mg". Otherwise the numeric dose will be wrong (like 125 in my example) because the decimal point will be deleted.
You could, for example, list all characters to be deleted in the second argument of the COMPRESS function:
want=input(compress(drug,'gmµu'),best.);
This would correctly transform values including a decimal point and still remove units "mg", "g", "ug" or "µg".
Might be easier to add the decimal indicator to the list of Kept characters.
input(compress(drug,'.','kd'),12.);
Thanks @data_null__ for chiming in. I had considered this option, too, but I was thinking that the period might occur in abbreviations (like "gr."). In this case, however, COMPRESS alone wouldn't work anyway (unlike SCAN, PRXCHANGE, ...).
@FreelanceReinh yes there are many possibilities that are not being accounted for. Using SCAN with those same options accounts for your latest scenario.
Good point. Cases like "15mg" or "0.5g" are treated correctly as well.
Thank you data_null, the prior code resulted in large unexpected numbers, I appreciate the follow-up.
@lillymaginta wrote:
Thank you data_null, the prior code resulted in large unexpected numbers, I appreciate the follow-up.
Not sure what you're talking about. What prior code? What unexpected numbers?
@lillymaginta I was about to edit the topic title to fix spelling...but then I SEE WHAT YOU DID THERE. 😉
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.