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

I want to validate arithmetic/Mathematical expressions. expression is a string that can be given to program via any file or via datalines/cards. 

 

 

data test;
length expression $15;
input expression $;
datalines;                      
(10+5)/(10-7)
1+2
2-3/4
(6-10)*6
c-10
10+-5
10/0
(10-40-)10
(K100-50)/10
(10+50)15
10*/5 ; run;

 

 

 

from the above expression some are correct and few are incorrect. I want to validate each expression as

 

Below is what I thought and tried

 

data validation;

set test;

length validate 8;

validate = function(expresssion);

run;

 

function can be any macro or function that runs that executes the expression and saves the input in validate. if the value of validate is not empty than the  expression is correct else the expression is incorrect. 

 

I've tried 

 

validate = put(expression , 8.);
validate = expression;

but it's not working as expected. 

 

below is the expression validation

 

"(10+5)/(10-7)" */Correct/*
"1+2" */Correct/*
"2-3/4" */Correct/*
"(6-10)*6" */Correct/*
"c-10" */InCorrect/*
"10+-5" */Correct/*
"10/0" */InCorrect/*
"(10-40-)10" */InCorrect/*
"(K12 - 50)/10" */InCorrect/*
"(100+50)15" */InCorrect/*

"10*/5" */InCorrect/*

 

Kindly provide an approach to validate arithmetic expressions  that are stored in character variables.

 

Thanks.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

This could give you a clue .

 

data test;
length expression $15;
input expression $;
result=resolve(cats('%sysevalf(',expression,')'));
datalines;                      
(10+5)/(10-7)
1+2
2-3/4
(6-10)*6
c-10
10+-5
10/0
(10-40-)10
(K100-50)/10
(10+50)15
;
run;

View solution in original post

3 REPLIES 3
Ksharp
Super User

This could give you a clue .

 

data test;
length expression $15;
input expression $;
result=resolve(cats('%sysevalf(',expression,')'));
datalines;                      
(10+5)/(10-7)
1+2
2-3/4
(6-10)*6
c-10
10+-5
10/0
(10-40-)10
(K100-50)/10
(10+50)15
;
run;
ballardw
Super User

I will beg to differ on these:

"c-10" */InCorrect/*  :<= this would be variable C subtracting 10. Why is this incorrect?

"10+-5" */InCorrect/* :<= this is 10 plus a negative 5

4    data junk;
5       x = 10+-5;
6    run;

NOTE: The data set WORK.JUNK has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.00 seconds

and yields the expected value of 5

"(K12 - 50)/10" */InCorrect/ :<= variable K12 minus 50, the quantity divided by 10. So why is this incorrect. If the variable K12 is not defined then 1) just created it, 2) effectively assigned a missing value and 3) a missing value results from the expression. Note the log below shows 2 variables in the output data.

11   data junk;
12      y=(K12 - 50)/10;
13   run;

NOTE: Variable K12 is uninitialized.
NOTE: Missing values were generated as a result of performing
      an operation on missing values.
      Each place is given by:
      (Number of times) at (Line):(Column).
      1 at 12:11
NOTE: The data set WORK.JUNK has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds

 

If this is for a grammar other than SAS you  need to be much more explicit about why these are incorrect, and SAS may not be the tool to evaluate.

 

Azeem112
Quartz | Level 8

Regarding "C-10" and "(K12 - 50)/10" are incorrect because variables are not allowed and system don't ave values for these variables. just numeric arithmetic expression. 

Regarding 10+-5 is correct, I've edited the post. however 10+*5 or 10/*5 where two operators are coming together except "10+-5", "10*-5" and "10/-5" are incorrect.

Thanks.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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
  • 3 replies
  • 1948 views
  • 1 like
  • 3 in conversation