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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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