DATA Step, Macro, Functions and more

Extract digits only

Reply
Super Contributor
Posts: 673

Extract digits only

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,
Contributor
Posts: 36

Re: Extract digits only

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.
Super Contributor
Posts: 673

Re: Extract digits only

Thanks for your response.
Trusted Advisor
Posts: 2,116

Re: Extract digits only

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
Super Contributor
Posts: 673

Re: Extract digits only

Hi Doc@duke,
Thanks for the help.Yes there is ambiguity regarding this situation drug 10/80.
Super Contributor
Posts: 474

Re: Extract digits only

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.
Ask a Question
Discussion stats
  • 5 replies
  • 132 views
  • 0 likes
  • 4 in conversation