Your SAS programs, embedded in web apps and elsewhere

How to evaluate an numeric expression

Accepted Solution Solved
Reply
Contributor
Posts: 28
Accepted Solution

How to evaluate an numeric expression

[ Edited ]

Sorry for kind of a SAS noob question here, but here goes:

 

I have a simple Stored Process based on an example from the DI Studio user guide, which converts a temperature.  I've simplified it to be a stand-alone Stored Process with Inputs/Outputs defined on the Stored Process itself.  I'm then accessing it via the SASBIWS "json" URL that we get for any Stored Process when we have SASBIWS.  Anyway - here's the Store Proc code:

%global INTEMP INUNIT OUTTEMP OUTUNIT;

%STPBEGIN;

%macro convertTemp;

%let OUTTEMP = -9999.9;
%let OUTUNIT = "";

%if "&INUNIT" = "F" %then
  %do;
    %let OUTTEMP = (5.0/9.0)*((&INTEMP)-32.0);
    %let OUTUNIT = "C";
  %end;
%else %if "&INUNIT" = "C" %then
  %do;
    %let OUTTEMP = ((9.0/5.0)*(&INTEMP))+32.0;
    %let OUTUNIT = "F";
  %end;

%mend;

%convertTemp;
%put OUTTEMP parameter is &OUTTEMP;
%put OUTUNIT parameter is &OUTUNIT;

%STPEND(debug=y);

INTEMP is defined as a Numeric (double) type; INUNIT is Text type.

So... pretty straightforward.  When I call it from a browser using.

https://ourserver/SASBIWS/json/storedProcesses/path/StoredProcessName?INTEMP=100&INUNIT=C

... the response is:

{"outputParameters":{"OUTTEMP":"((9.0/5.0)*(100.0))+32.0","OUTUNIT":"\"F\""}}

So... it handles the string INUNIT parameter: it is "C" going in, and the conditional logic gives the "F" in the response.

 

But my question is why doesn't it evaluate the numeric expression: ((9.0/5.0)*(100.0))+32.0 ?

 

Thanks!


Accepted Solutions
Solution
3 weeks ago
Super User
Posts: 5,085

Re: How to evaluate an numeric expression

As you have seen, macro language doesn't perform math automatically as part of a %LET statement.  There are some places within macro language where it will perform the math, but not as part of %LET.  So you have to tell it to perform the math:

 

%let OUTTEMP = %sysevalf((5.0/9.0)*((&INTEMP)-32.0)) ;

View solution in original post


All Replies
Solution
3 weeks ago
Super User
Posts: 5,085

Re: How to evaluate an numeric expression

As you have seen, macro language doesn't perform math automatically as part of a %LET statement.  There are some places within macro language where it will perform the math, but not as part of %LET.  So you have to tell it to perform the math:

 

%let OUTTEMP = %sysevalf((5.0/9.0)*((&INTEMP)-32.0)) ;

Trusted Advisor
Posts: 1,616

Re: How to evaluate an numeric expression

This is very simple to do in a DATA step, there's no reason for a macro at all.

Contributor
Posts: 28

Re: How to evaluate an numeric expression

Well, same question: I can write "non-macro" code, but I have the same issues processing any of the data:

%global INTEMP INUNIT OUTTEMP OUTUNIT;

%STPBEGIN;

data _null_;

  OUTTEMP = 999;
  OUTUNIT = "";

  if "&INUNIT" = "F" then
    do;
      OUTTEMP = (5.0/9.0)*((&INTEMP.)-32.0);
      OUTUNIT = "C";
    end;
  else if "&INUNIT" = "C" then
    do;
      OUTTEMP = ((9.0/5.0)*(&INTEMP.))+32.0;
      OUTUNIT = "F";
    end;

run;

%put OUTTEMP parameter is &OUTTEMP;
%put OUTUNIT parameter is &OUTUNIT;

%STPEND;

When called from a browser, this returns null values:

{"outputParameters":{"OUTTEMP":"","OUTUNIT":""}}

Still being quite new to SAS, I can't spot what's missing... I've already tried several variations; some give syntax errors; others behave just like this one: running but providing null results.

Contributor
Posts: 28

Re: How to evaluate an numeric expression

Kinda half and half: this allows me to remove the macro conditional logic, but I still appear to need the macro "%let" statements.

%global INTEMP INUNIT OUTTEMP OUTUNIT;
%STPBEGIN;
%let OUTTEMP = 999;
%let OUTUNIT = "";
data _null_;
  if "&INUNIT" = "F" then
    do;
      %let OUTTEMP = %sysevalf((5.0/9.0)*((&INTEMP.)-32.0));
      %let OUTUNIT = "C";
    end;
  else if "&INUNIT" = "C" then
    do;
      %let OUTTEMP = %sysevalf(((9.0/5.0)*(&INTEMP.))+32.0);
      %let OUTUNIT = "F";
    end;
run;
%put OUTTEMP parameter is &OUTTEMP;
%put OUTUNIT parameter is &OUTUNIT;
%STPEND;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 283 views
  • 1 like
  • 3 in conversation