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

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

 

1 ACCEPTED SOLUTION
13 REPLIES 13
art297
Opal | Level 21

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
;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Do please like the original post from Ksharp, its his solution after all Smiley Happy

ballardw
Super User

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.

novinosrin
Tourmaline | Level 20

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 

ballardw
Super User

@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.

novinosrin
Tourmaline | Level 20

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!

art297
Opal | Level 21

@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

 

ballardw
Super User

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 +.

 

art297
Opal | Level 21

@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.

 

ballardw
Super User

@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

art297
Opal | Level 21

@ballardw: Yes, that was the only one that began with -1,

 

Art

 

Ready to join fellow brilliant minds for the SAS Hackathon?

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!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 13 replies
  • 1230 views
  • 4 likes
  • 4 in conversation