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!
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)) ;
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)) ;
This is very simple to do in a DATA step, there's no reason for a macro at all.
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.