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.
or
data w;
k='1-2 times in 1 week';
want=scan(compress(k,' ','kdp'),1);
run;
data want;
set have;
want=char(var,anydigit(var));
run;
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?
sorry my fault. brb
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;
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
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
or
data w;
k='1-2 times in 1 week';
want=scan(compress(k,' ','kdp'),1);
run;
Thanks a lot for the valuable advice.
It helps.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.