Hi there,
I am stuck on a problem. I have a character variable which contains the values from a questionnaire. I have no way to tell if the question asks for a dollar amount other than seeing the value (i.e. 1.99). Is there any function I can use to evaluate the value to determine if it contains a decimal value only? I should note that I cannot use the ANYDIGIT function because I have values that are numbers that are not dollar amounts and I have decimal values attached to character strings which are not indicative of a dollar amount. I just want to identify the values that contain numbers and a decimal only.
data have;
input char_var $;
datalines;
APPLE
BROWN
4.99
BALL2.0
568.0000
69833
;
run;
I would love the end result to look like this:
CHAR_VAR DECIMAL_VALUE
APPLE No
BROWN No
4.99 Yes
BALL2.0 No
568.0000 Yes
69833 No
Any suggestions? I am using SAS 9.2.
Thanks in advance.
One easy way is to test if SAS thinks it is a number by using INPUT() functio
data have;
input char_var $20.;
datalines;
APPLE
BROWN
4.99
BALL2.0
568.0000
69833
1.00
;
data want;
set have ;
if index(char_var,'.') and not missing(input(char_var,??32.)) then decimal_value='Yes';
else decimal_value='No ';
run;
decimal_ Obs char_var value 1 APPLE No 2 BROWN No 3 4.99 Yes 4 BALL2.0 No 5 568.0000 Yes 6 69833 No 7 1.00 Yes
not anyalpha(char_var);
Thank you for your suggestion PG, but this function returns obs 6 (69833) which is not a decimal value.
Thanks,
Jasmin
This looks like a PRXMATCH would likely work. I'm not very good but this seems to work for your example:
data have; input char_var $; if _N_=1 then do; retain PerlExpression; pattern="/(\d+?)[.](\d+?)/"; PerlExpression=prxparse(pattern); end; decimal_value = (prxmatch(PerlExpression,Char_var) = 1); datalines; APPLE BROWN 4.99 BALL2.0 568.0000 69833 ; run;
Using 1 for Yes / True and 0 for no/ false.
Thank you ballardw. This solution works very well on my real dataset. This is exactly what I was looking for!
Thanks,
Jasmin
One easy way is to test if SAS thinks it is a number by using INPUT() functio
data have;
input char_var $20.;
datalines;
APPLE
BROWN
4.99
BALL2.0
568.0000
69833
1.00
;
data want;
set have ;
if index(char_var,'.') and not missing(input(char_var,??32.)) then decimal_value='Yes';
else decimal_value='No ';
run;
decimal_ Obs char_var value 1 APPLE No 2 BROWN No 3 4.99 Yes 4 BALL2.0 No 5 568.0000 Yes 6 69833 No 7 1.00 Yes
Thank you Tom. Your solution works great on my real data set. I love the simplicity of your code!
Thanks,
Jasmin
data have;
input char_var $20.;
datalines;
APPLE
BROWN
4.99
BALL2.0
568.0000
69833
1.00
;
data want;
set have;
if prxmatch('/^\d*\.\d*$/',strip(char_var)) then flag=1;
else flag=0;
run;
Thank you Ksharp. Your solution worked perfectly on my real dataset. I am impressed at how many solutions there are to this problem. I like how concise your solution is.
Thanks,
Jasmin
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.