Hi.
I have a dataset in which one of the variable has arithmetic expression. These expression are entered by user and it contains syntax errors. See dataset below where observation 2,3 and 4 are incorrect expression. If I use sysevalf or evalf function to evaluate them it throws the syntax error as below.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required.
data have;
input exp $20.;
cards;
701*10 ** 6
4..60*10 ** 6
6t.22 * 10**-4
2*10**-3s
;
run;
IS there any function to parse these expression befor applying the sysevalf function. Please help.
Thanks,
Anand
There are a lot of pattern you need to match. This one could relieve your task.
data have;
input exp $20.;
if prxmatch('/^((-?\d+(\.\d+)?)(\+|-|\*{1,2}|\/)?)+$/',compress(exp)) then
x=resolve(cats('%sysevalf(',exp,')'));
cards;
701*10 ** 6
4..60*10 ** 6
6t.22 * 10**-4
2*10**-3s
;
run;
Thank you Both.
Regular expression solution is working partially. But its not working for all the formulas. I am not well versed in regular expression so trying to fine tune it.
for e.g. if the rule is like this (45.25/46.00*23.06). This contains the open and close braces. Its not evaluating such rules.
Depending on what you consider a solution to be, consider this approach:
data _null_;
call execute('data _null_; set have; file print;');
do until (done);
set have end=done;
call execute ('testvar = ' || exp || '; put exp @ ;');
call execute ('if testvar=. then put @25 "Result: Not valid"; else put @25 "Result: Valid";')
end;
call execute('run;');
run;
The intent is to generate a report showing whether each expression is valid or not. I'm just not sure if that is an acceptable outcome or not. Also note, the code is untested so it may need slight tweaking.
Good luck.
This solution is failing as we are really not checking the rule prior to the execution. Directly executing the rule is throwing the syntax error.
OK, one last try at what I think you need to solve here. It is only being applied to one formula at a time, since it appears that you need to decide whether or not to proceed with processing based on whether the formula is valid.
data _null_;
input exp $20.;
call symputx('formula', exp);
cards;
701*10 ** 6
;
%let proceed=Y;
data _null_;
check = &formula;
if check=. then call symputx('proceed', 'N');
run;
%if &syserr > 0 %then %let proceed=N;
This gives you a macro variable &PROCEED that will be Y if you are safe, but N if either bad outcome is present: the input formula contains invalid syntax, or the input formula generates a missing value (possibly because of referring to an unknown variable name).
Are we getting close?
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.