BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nicolaskan
Fluorite | Level 6

Hi guys,

 

May i know if there are any simple way to extract first numeric digit in a string with mix of number and string?

 

Example:

 

Column: Text1

Value of Text1: "2 times in 1 week"

Expected Output: 2

 

I have tried to use the following script yet it combines two numeric value:

 

Output_value = COMPRESS(SCAN(Text1,1),"abcdefghijklmnopqrstuvwxyz`~*!@#$%^*()-_=+\|[]{};:',.<>?/ " , "i");

=> Output: 21 (i.e. I would like to extract 2 only)

 

May i know if anyone can help?

Thanks a lot.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

or

 

data w;
k='1-2 times in 1 week';
want=scan(compress(k,' ','kdp'),1);
run;

View solution in original post

8 REPLIES 8
novinosrin
Tourmaline | Level 20

data want;

set have;

want=char(var,anydigit(var));

run;

 

nicolaskan
Fluorite | Level 6

Thanks for the quick reply.

however, it doesn't solve the case like

"10 times in 1 week"

=> it will output 1 instead of 10 in this case.

 

Any idea?

novinosrin
Tourmaline | Level 20

my sincere apologies for the lack of attention to detail

 

data w;
k='"10 times in 1 week"';
want=scan(compress(k,' ','kd'),1);
run;
nicolaskan
Fluorite | Level 6

Many thanks for the advice.

 

Some complicated situation as follows couldn't be solved.

Any advice?

 

"1-2 times in 1 week"

Expected Output:1

Using the above code: 12

 

 

novinosrin
Tourmaline | Level 20
data w;
k='1-2 times in 1 week';
want=scan(compress(k,'  -','kd'),1);
run;

better is to apply regex though using prx functions

novinosrin
Tourmaline | Level 20

or

 

data w;
k='1-2 times in 1 week';
want=scan(compress(k,' ','kdp'),1);
run;
nicolaskan
Fluorite | Level 6

Thanks a lot for the valuable advice.

It helps.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 7966 views
  • 1 like
  • 2 in conversation