BookmarkSubscribeRSS Feed
Ronein
Meteorite | Level 14

Hello

What is the way to take only numeric (numbers) from a string ?

For example:

Data t;

input string;

cards;

abcd25_fmt

gfhhff31_qr

jdhh4gt_r

;

run;

 

I need to  get:

25

31

4

 

5 REPLIES 5
A_Swoosh
Quartz | Level 8
You can achieve this by using a compress function to eliminate non-numeric values.

want= compress(string,'','A');
Ronein
Meteorite | Level 14
What does it mean 'A'?
What is the meaning of ''?
I ask in order to have logic
A_Swoosh
Quartz | Level 8
Here is a good link for documentation on what you can do with the modifiers in the compress function.

https://www.lexjansen.com/phuse/2016/cc/CC01.

The third parameter, 'A' gets rid of all (A-Z) and (a-z) ...while the second parameter, ' ', does nothing other than hold space for the third parameter.
KlausBücher
Fluorite | Level 6

Another way is to use the compress function with "k" and a positiv list:

 

want= compress(string,'0123456789','k');

 

For details of the compress function, see: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

 

Astounding
PROC Star

The correct answer actually depends on details that you haven't specified.  What about decimal points?  What about minus signs?  What about multiple sets of digits?  What should each of these become?

 

gfhhff3.14_qr

gfhhff3a1_qr

gfhhff-31_qr

gfhhff3-1_qr

gfhhff3+1_qr

gfhhff+31_qr

 

If you want to learn about the COMPRESS function (such as the meaning of "A"), you can read it here:

 

https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.5&docsetId=lefunctionsref&docsetTarg...

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 3104 views
  • 1 like
  • 4 in conversation