BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
Hi All,
I need to extract only digits from a variable.Say for instance I have a field "drug name" with values Symbort 25 mg, Avalide 125 ml,Xyntha 1050.
I need only 25,125 1050.
Thanks for your time and help,
5 REPLIES 5
barheat
Fluorite | Level 6
You can use the compress function to strip all of the characters. All the letters of the alphabet are listed in both upper and lower case within quotes. The result will be a character variable containing only the digits. You can then use an input function to convert the character to a numeric.

data file;
set file;
number=compress(drug_name,'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ');
run;

I'm sure that there is a more elegant solution but this one works.
SASPhile
Quartz | Level 8
Thanks for your response.
Doc_Duke
Rhodochrosite | Level 12
compress(drug_name,,a) gives the same results. See the documentation for COMPRESS.0

What about fields with two numbers int hem? Say "Vytorin 10/80'? The code above gives '10/80' as the result but that may not be what you want. COMPRESS has lots of options to give you more control on the parsing.

Doc Muhlbaier
Duke
SASPhile
Quartz | Level 8
Hi Doc@duke,
Thanks for the help.Yes there is ambiguity regarding this situation drug 10/80.
DanielSantos
Barite | Level 11
You could use the regular expression functions to extract the digits, but as stated before, COMPRESS function would be most direct and simple way

Check the online documentation for the compress function (and its modifiers):

http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000212246.htm

Greetings from Portugal.

Daniel Santos at www.cgd.pt.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 5 replies
  • 1722 views
  • 0 likes
  • 4 in conversation