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

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!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

4 REPLIES 4
Astounding
PROC Star

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)) ;

PaigeMiller
Diamond | Level 26

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

--
Paige Miller
JohnJPS
Quartz | Level 8

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.

JohnJPS
Quartz | Level 8

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;

sas-innovate-wordmark-2025-midnight.png

Register Today!

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.


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