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
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.