BookmarkSubscribeRSS Feed

Bullet Proof your Application - Checking validity of input table

Started ‎07-09-2020 by
Modified ‎07-09-2020 by
Views 1,524

In many applications the users specifies the the input table. In CAS, the program can fail for one of the following reasons (among others)

  • table exists but the table is not loaded
  • table does not exist
  • Caslib is incorrect

A good program should check the input and give users a friendly message if there is a problem. The program should not make users crawl through logs to find out what happened. Below is a simple CASL function checkAndLoadTable that I use in my applications and libraries like @sassoftware/restaflib to make sure that the table is loaded and is ready to go. If necessary, the function will load the table.

 

The code is available in the attached file.

 

Typical usage

 

rc = checkAndLoadTable(caslib, name);
if ( rc ne true) then do;
   ... handle error...
end;
else do;
   ... real work ...
end;

 

Test program with sample output

For your convenience here is a simple test program and sample results.

 

function test(testName, caslib, name) ;
    print '-----------------------------------------------------';
    print testName;
    print '-----------------------------------------------------';
	print 'Input caslib.name is: ' caslib '.' name;
	result = checkAndLoadTable(caslib, name);
	print 'Returned value: ' result;
	if (result  eq true) then do;
	   action table.recordCount r=result/
	      table = {caslib=caslib, name=name};
       print 'Record Count: ' result.recordCount[1,1];
	end;
 
end;

 

Below is a sample run and output.

 

test('Loaded Table','systemData', 'postgres');
test('Table not pre-loaded','casuser', 'iris');
test('Bad caslib', 'x', 'iris');
test('Bad table', 'casuser', 'x');

and the sample output is
-----------------------------------------------------
Loaded Table
-----------------------------------------------------
Input caslib.name is: systemData.postgres
Returned value: TRUE
Record Count: 4015
-----------------------------------------------------
Table not pre-loaded
-----------------------------------------------------
Input caslib.name is: casuser.iris
Returned value: TRUE
Record Count: 150
-----------------------------------------------------
Bad caslib
-----------------------------------------------------
Input caslib.name is: x.iris
Returned value: -1
-----------------------------------------------------
Bad table
-----------------------------------------------------
Input caslib.name is: casuser.x
Returned value: FALSE

test('Promoted Table','systemData', 'postgres');
test('Table not loaded','casuser', 'iris');
test('Bad caslib', 'x', 'iris');
test('Bad table', 'casuser', 'x');

 

Finally

This function can be used inside PROC CAS or in any CASL program running on the CAS server. If your application is used in spurts the checkAndLoadTable function is a good way to load the tables on demand. Remember that defensive programming saves hours of debugging for the user. Cheers. Deva
Version history
Last update:
‎07-09-2020 08:06 AM
Updated by:
Contributors

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

SAS AI and Machine Learning Courses

The rapid growth of AI technologies is driving an AI skills gap and demand for AI talent. Ready to grow your AI literacy? SAS offers free ways to get started for beginners, business leaders, and analytics professionals of all skill levels. Your future self will thank you.

Get started

Article Tags