BookmarkSubscribeRSS Feed

Bullet Proof your Application - Checking validity of input table

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

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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