DATA Step, Macro, Functions and more

Test if a character variable contains a decimal value

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Test if a character variable contains a decimal value

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.


Accepted Solutions
Solution
‎11-17-2017 09:20 AM
Super User
Super User
Posts: 8,279

Re: Test if a character variable contains a decimal value

[ Edited ]
Posted in reply to sas-inquirer

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

 

 

View solution in original post


All Replies
Esteemed Advisor
Posts: 5,624

Re: Test if a character variable contains a decimal value

Posted in reply to sas-inquirer

not anyalpha(char_var);

PG
Contributor
Posts: 46

Re: Test if a character variable contains a decimal value

Thank you for your suggestion PG, but this function returns obs 6 (69833) which is not a decimal value.

 

Thanks,

Jasmin

Super User
Posts: 13,941

Re: Test if a character variable contains a decimal value

Posted in reply to sas-inquirer

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.

 

Contributor
Posts: 46

Re: Test if a character variable contains a decimal value

Thank you ballardw.  This solution works very well on my real dataset. This is exactly what I was looking for!

 

Thanks,

Jasmin

Solution
‎11-17-2017 09:20 AM
Super User
Super User
Posts: 8,279

Re: Test if a character variable contains a decimal value

[ Edited ]
Posted in reply to sas-inquirer

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

 

 

Contributor
Posts: 46

Re: Test if a character variable contains a decimal value

Thank you Tom. Your solution works great on my real data set. I love the simplicity of your code!

 

Thanks,
Jasmin

Super User
Posts: 10,849

Re: Test if a character variable contains a decimal value

Posted in reply to sas-inquirer
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;
Contributor
Posts: 46

Re: Test if a character variable contains a decimal value

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 276 views
  • 4 likes
  • 5 in conversation