Simple Error Handling and Logging Strategy using SAS Studio
The Power to Know - SAS
When you write your first SAS programs using SAS Studio, sometimes you are asked to do Data Validation to grant essential Data Quality before the reporting phases. So by working on a Risk Data Project for regulatory Reporting, I have faced the need to enforce extra data Validation on top of an existing Data Validation Layer before executing the Load process to Historical Data Sets used for reporting on a Front-End Application or Client Application.
As a developer on the team of Data Preparation far from the SAS Data Integration Studio Developers Team, and surrounded by starving data Business users involved in the process with the need of not relying too much on more technical personnel and constantly asking for justifications, I came up with an idea of creating a friendly solution and easy to use using SAS Studio for error Logging.
Why?
Business Users want Simple to Use solutions to deal with useful and quality Data before submitting their Reports.
What?
The focus is on a Business User Perspective, who relies on well assessed and prepared input data for Model Execution.
SAS Studio is the tool to achieve the goal on a seamlessly manner with a Graphical User Interface (GUI) feature called Process Flow.
And lastly, a Validation Log Report Generation using Output Delivery System (ODS).
Summary
The focus will be how we as SAS Programmers can offer a simple solution to a Business user to be as comfortable with his input data as possible, before executing Models or Internal Processes for Regulatory Reporting.
A simple Process Flow will be shared to show you how you can organize your activities in a Sequential Step Flow to ensure Error Handling accordingly and generate a log report to share with the Data Integration developers in a fashion they can easily understand similar to SAS Data Integration Studio Job Flow Diagram to allow quick corrections on the JOB applications.
Use the Programming Interface: SAS Studio
First Things First:
In SAS Data Integration Studio, developers in a very but very simplified manner, create jobs, and jobs are sets of input data extractions, transformation and normally Output Table Loaders to create Output Information. A very essential Extraction, Transformation and Load concepts applied (ETL), but there is much more than that for future ArticlesJ.
Each of Extraction, Transformation and load action are organized in sequential Process Flow.
Figure 1. Sample Job Process Flow
So, in order to perform error handling and logging in SAS on a try/catch fashion used in Object Oriented Programming, you need to organize your statements in sequential Steps Just like is done in SAS Data Integration Studio, making it easier to catch the output error codes and messages to and write it to a Temporary/Permanent Dataset for reporting or logging.
Conclusions
The Idea focus on performing error handling in SAS on try/catch fashion used in Object Oriented Programming and by using Control Program Flow feature in SAS Studio, you can generate an interactive and easy to Graphical User Interface (GUI) to your business users even if they are not proficient in SAS Programming.
Figure 2.Process Flow Example
At the end of the flow, you can generate an execution Log or an Execution Report for monitoring or audit assessment.
References
I would recommend the following resources:
https://communities.sas.com/t5/SAS-Programming/Error-Handling-in-a-construct-like-try-catch/td-p/539151#
https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n1wrevo4roqsnxn1fbd9yezxvv9k.htm&docsetVersion=9.4&locale=en
https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p011imau3tm4jen1us2a45cyenz9.htm&docsetVersion=9.4&locale=en
SAS Output Delivery System: User's Guide
SAS® Data Integration Studio 4.904: User's Guide
Strategies for Error Handling and Program Control: Concepts - Paper 1565-2015
Want to try yourself the content of this post?
Code Sample
The following code simulates Two Activities:
The First task I present is the backup of an Input Dataset before manipulation or Changes in the Validation and Assessment process, before loading it to Permanent Datasets.
So I started by creating a SAS program and saving it in a specific folder on My SAS Server.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/* Task01:Backup Input Data Set before Changes */
/*Lib LOG Folder*/
libname LOG '/XXXX/XXXXXXX/XXXXX/LogRepporting';
/*Lib Backup Folder*/
libname Backup '/XXXXX/XXXXX/XXXXXXX/LogRepporting';
%macro logDataSet(dsn);
%if %sysfunc(exist(&dsn)) %then
%do;
/***exists***/
%end;
%else
%do;
data log.Task01;
attrib Task length=$8;
attrib Description length=$256;
attrib LIB length=$256;
attrib STEP length=$256;
attrib ErrorCode length=8;
call missing(of _all_);
stop;
run;
%end;
%mend;
%logDataSet(log.Task01);
/*Step1: Backup before any change*/
data Backup.testefinal;
set CODAT01.testefinal;
run;
%let error=&syserr;
%let ErrorDescription=&syserrortext;
%let libr =&syslibrc;
proc sql;
select CASE WHEN &error=0 then 'Sucess' ELSE "&ErrorDescription" end as
STEP into :STEPST from log.Task01;
select CASE WHEN &libr=0 then 'Success' ELSE "&ErrorDescription" end as
LIB into :Library from log.Task01;
insert into log.Task01 values('Task01','Task01:Backup Input Data Set before Changes',"&Library","&STEPST", &error
);
quit;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Secondly and lastly, I present a task to create the Log for Analysis and Repporting
/* Task02:Log Generation for Analisys and Repporting*/
ods pdf file="/XXXXX/XXXXXX/XXXXXXX/LogRepporting/LogRepport.pdf" author="Carlos Spranger";
Title'Log and Repporting';
PROC REPORT DATA=Log.Task01 NOWINDOWS HEADLINE;
COLUMN Task Description Lib STEP ErrorCode;
DEFINE Task / display "Task Name" width=8;
DEFINE Description / display "Task Description" width=32;
DEFINE LIB / display "Lib Status" width=14;
DEFINE STEP / display "Step Status" width=8;
DEFINE ErrorCode / display "ErrorCode" width=1;
RUN;
ods pdf close;
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Then I create a Process Flow in Visual Designer Mode in SAS Studio and drag my SAS Programs to it.
Note that as good practice, I try to free resources just like the Dispose method used in most conventional Object Oriented Languages. Here a dispose My Datasets right after the report is successfully generated.
And then I can execute the logic in an End-User perspective:
Figure 3.Execute Process Flow Diagram in SAS Studio
... View more