I beforehand mentioned tips on how to use the PUTLOG assertion to put in writing a message from the DATA step to the log in SAS. The PUTLOG assertion is usually used to put in writing notes, warnings, and errors to the log.
This text exhibits tips on how to use the PRINTTOLOG subroutine in SAS IML software program to put in writing messages to the log.
The PRINTTOLOG subroutine
was launched in 2020 in SAS IML on SAS Viya. It runs in PROC IML and within the iml motion on Viya. The PRINTTOLOG subroutine just isn’t a part of SAS 9.4, however I present tips on how to assemble a SAS/IML module for SAS 9.4 that has the identical syntax and performance.
The PrintToLog subroutine in SAS IML on Viya
The syntax of the subroutine is
name PrintToLog(msg, flag);
The flag parameter is non-obligatory. In case you omit that argument, the message is displayed within the log. In case you use the
flag parameter, the legitimate values are 0, 1, and a pair of:
- If flag is 0, the string “NOTE: ” is prepended to the message. For instance, name PrintToLog("It is a observe.", 0);.
- If flag is 1, the string “WARNING: ” is prepended to the message. For instance,
name PrintToLog("It is a warning.", 1);. - If flag is 2, the string “ERROR: ” is prepended to the message.
For instance, name PrintToLog("That is an error.", 2);.
I’ll illustrate the routine through the use of an IML model of this system that I utilized in my earlier put up concerning the PUTLOG assertion.
The enter to this system is a three-column matrix. Every row of the matrix accommodates the coefficients (a, b, c) for the quadratic equation
a x2 + b x + 3 = 0. This system is vectorized. It makes use of the discriminant of the quadratic equation to find out whether or not the quadratic equation has any actual roots. If that’s the case, this system makes use of the quadratic system to search out the roots. To show the PrintToLog operate, this system does the next:
- Show an ERROR if any coefficient of the quadratic time period is 0.
- Show a WARNING if any quadratic equation doesn’t have any actual roots.
- Show a NOTE if any quadratic equation has a repeated root.
proc iml; /* NOTE: The PrintToLog subroutine is accessible in SAS IML in Viya. It isn't obtainable as a built-in subroutine for SAS/IML in SAS 9.4. */ Q = { 1 -1 -6, /* every row is (a,b,c) */ 1 7 6, 0 7 6, 2 7 7, -2 5 12, -2 4 -2, 5 4 10}; a = Q[,1]; b = Q[,2]; c = Q[,3]; Root1 = j(nrow(a), 1, .); /* initialize roots to lacking */ Root2 = j(nrow(a), 1, .); /* initialize roots to lacking */ if any(a=0) then name PrintToLog("The coefficient of the quadratic time period is 0 for no less than one statement", 2); /* show ERROR */ discrim = b##2 - 4*a#c; if any(discrim < 0) then name PrintToLog("A minimum of one equation doesn't have actual roots.", 1); /* show WARNING */ if any(discrim=0) then name PrintToLog("A minimum of one equation has repeated roots.", 0); /* show NOTE */ idx = loc(discrim >= 0 && a^=0); if ncol(idx)>0 then do; Root1[idx] = (-b[idx] - sqrt(discrim[idx])) / (2*a[idx]); Root2[idx] = (-b[idx] + sqrt(discrim[idx])) / (2*a[idx]); finish; print a b c discrim Root1 Root2; |
The SAS log accommodates the next message, which is color-coded based on whether or not the message is an error (purple coloration), a warning (inexperienced coloration), or a observe (blue coloration).
For completeness, the output of this system is proven under.
A PrintToLog module for SAS 9.4
Though the PrintToLog subroutine just isn’t a built-in subroutine in SAS 9.4, you’ll be able to outline a module that has the identical performance.
The definition makes use of two SAS/IML methods and one macro trick:
The next SAS macro defines a SAS/IML module in SAS 9.4, however not in SAS Viya. Thus, you’ll be able to run this macro in any PROC IML program. If this system runs in Viya (the place the PrintToLog subroutine is already outlined), the macro does nothing. If this system runs in SAS 9.4, the macro defines a PrintToLog module that you need to use to put in writing messages to the log.
/* Test the SYSVER macro to see if SAS 9.4 is operating. In SAS Viya, the macro is empty and does nothing. In SAS 9.4, the macro defines a operate that emulates the PrintToLog name. The syntax is as follows: name PrintToLog("It is a log message."); name PrintToLog("It is a observe.", 0); name PrintToLog("It is a warning.", 1); name PrintToLog("That is an error.", 2); */ %macro DefinePrintToLog; %if %sysevalf(&sysver = 9.4) %then %do; begin PrintToLog(msg,errCode=-1); if errCode=0 then prefix = "NOTE: "; else if errCode=1 then prefix = "WARNING: "; else if errCode=2 then prefix = "ERROR: "; else prefix = ""; stmt = '%put ' + prefix + msg + ';'; name execute(stmt); end; %finish; %mend; /* this program runs in SAS 9.4 or in SAS Viya */ proc iml; %DefinePrintToLog; name PrintToLog("It is a log message."); name PrintToLog("It is a observe.", 0); name PrintToLog("It is a warning.", 1); name PrintToLog("That is an error.", 2); |
The output is proven for SAS 9.4, the place the %DefinePrintToLog macro defines the SAS/IML module.
Abstract
The PRINTTOLOG assertion is manner for SAS IML programmers to put in writing messages to the log.
In SAS Viya, this subroutine is built-in and can be utilized from PROC IML or from the iml motion.
This text exhibits tips on how to outline a module for PROC IML in SAS 9.4 that has related performance.