Is there a function that performs a calculation described in a string? E.g., given the following code, is there a function that will provide 8 as the value of x?:
data test;
input formula $25.;
*x=???(formula);
cards;
-1+3+6
;
TIA,
Art
Ksharp shows two ways in this post:
https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/td-p/68123
Ksharp shows two ways in this post:
https://communities.sas.com/t5/SAS-Programming/resolving-a-formula-in-a-dataset/td-p/68123
Thanks! I went with his suggestion of using the resolve function. i.e.:
data test;
input formula $25.;
x=resolve('%sysevalf('||formula||')');
cards;
-1+3+6
;
Do please like the original post from Ksharp, its his solution after all
@RW9: Did it!
Only thing I can think of like that is placing the value in a macro variable and using %sysevalf (or %eval if integers only). Which I doubt really helps a lot.
fun with APP
data test;
input formula $25.;
*x=???(formula);
x1=translate(formula,' ','+');
array target(3)$2 _temporary_;
call pokelong (x1, addrlong(target[1]), 6) ;
want=sum(of target(*));
drop x1;
cards;
-1+3+6
;
EDITed to 'long' 64 bit machine
@novinosrin wrote:
fun with APP
data test; input formula $25.; *x=???(formula); x1=translate(formula,' ','+'); array target(3)$2 _temporary_; call pokelong (x1, addrlong(target[1]), 6) ; want=sum(of target(*)); drop x1; cards; -1+3+6 ;
EDITed to 'long' 64 bit machine
with this we need to know if @art297 needs to consider other functions such as multiplication, division, exponentiation and possibly grouping with () within the function string.
I have sort of wondered for quite awhile why the macro language has %eval and later %sysevalf without equivalents in the data step.
Thank you sir @ballardw for the response. Actually APP does not scale like 'resolve' for this thread. I realised that later but still didn't want to delete my post.
I suppose if any of those arthimetic math operators are in the formula %sysevalf should accomplish. Perhaps, the equivalents didn;t matter so long as the datastep interface with the macro facility like resolve, call execute, symget etc are giving a helping hand.
Btw, you took a week or so off? My classmate is still thankful to you for the regression plot of confidence level help. Dankie!
@ballardw: No. Just addition and subtraction. I'm trying to solve the Canadian FriendsOfSAS problem of the week that can be found at:
https://www.sas.com/content/dam/SAS/documents/technical/education/en/en-programming-problem-6.pdf
So one question becomes are
+1+2+34-5+67-8+9
and
1+2+34-5+67-8+9
different solutions or not?
I have one solution at 33 lines of code finding 23 answers if the answer to the above is "Yes they are different solutions". But 22 of them are pairs with and without the leading +.
@ballardw: Not sure how to answer your question because the first digit might have a leading negative sign. The solution I offered can be found at https://communities.sas.com/t5/Friends-of-SAS-Programming/November-14-2018-Programming-Problem/m-p/5..., but a much better one was also posted there.
However, both the one I liked and my own each found 11 equations that met the challenge's criteria. My guess is that your 22 solutions incorporate 11 duplicates. I have no idea where your 23rd solution might have come from.
@art297 wrote:
@ballardw: Not sure how to answer your question because the first digit might have a leading negative sign. The solution I offered can be found at https://communities.sas.com/t5/Friends-of-SAS-Programming/November-14-2018-Programming-Problem/m-p/5..., but a much better one was also posted there.
However, both the one I liked and my own each found 11 equations that met the challenge's criteria. My guess is that your 22 solutions incorporate 11 duplicates. I have no idea where your 23rd solution might have come from.
Yes there were 11 pairs, the odd man out
-1+2-3+4+5+6+78+9
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.