BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lillymaginta
Lapis Lazuli | Level 10
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 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26
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.

View solution in original post

10 REPLIES 10
RW9
Diamond | Level 26 RW9
Diamond | Level 26
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.

lillymaginta
Lapis Lazuli | Level 10

Thank you RW9 for the prompt response! 

FreelanceReinh
Jade | Level 19

@lillymaginta:

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".

data_null__
Jade | Level 19

@FreelanceReinh wrote:

@lillymaginta:

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.);
FreelanceReinh
Jade | Level 19

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, ...).

data_null__
Jade | Level 19

@FreelanceReinh yes there are many possibilities that are not being accounted for.  Using SCAN with those same options accounts for your latest scenario.

 

Capture.PNG

FreelanceReinh
Jade | Level 19

Good point. Cases like "15mg" or "0.5g" are treated correctly as well.

lillymaginta
Lapis Lazuli | Level 10

Thank you data_null, the prior code resulted in large unexpected numbers, I appreciate the follow-up.

 

data_null__
Jade | Level 19

@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?

ChrisHemedinger
Community Manager

@lillymaginta I was about to edit the topic title to fix spelling...but then I SEE WHAT YOU DID THERE. 😉

Learn from the Experts! Check out the huge catalog of free sessions in the Ask the Expert webinar series.

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
  • 10 replies
  • 1832 views
  • 10 likes
  • 5 in conversation