BookmarkSubscribeRSS Feed

Simple Error Handling and Logging Strategy using SAS Studio

Started ‎02-17-2020 by
Modified ‎08-04-2021 by
Views 4,277

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.



Business Users want Simple to Use solutions to deal with useful and quality Data before submitting their Reports.



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



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.




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.




I would recommend the following resources:



Want to try yourself the content of this post?

Code Sample


The following code simulates Two Activities:


  1. 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
                                               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_);
/*Step1: Backup before any change*/
data Backup.testefinal;
                set CODAT01.testefinal;
%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




  1. 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';
                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;
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


Anybody ever figure out how to do a prompt in SAS Studio?  Seems like that would be a simple yet vital 'feature'.

Version history
Last update:
‎08-04-2021 01:26 PM
Updated by:



Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

Free course: Data Literacy Essentials

Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning  and boost your career prospects.

Get Started

Article Tags