BookmarkSubscribeRSS Feed

Bullet Proof your Application - Checking validity of input table

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

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 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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