BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sas-inquirer
Quartz | Level 8

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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

8 REPLIES 8
PGStats
Opal | Level 21

not anyalpha(char_var);

PG
sas-inquirer
Quartz | Level 8

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

 

Thanks,

Jasmin

ballardw
Super User

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.

 

sas-inquirer
Quartz | Level 8

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

 

Thanks,

Jasmin

Tom
Super User Tom
Super User

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

 

 

sas-inquirer
Quartz | Level 8

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

 

Thanks,
Jasmin

Ksharp
Super User
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;
sas-inquirer
Quartz | Level 8

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: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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