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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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