In many applications the users specifies the the input table. In CAS, the program can fail for one of the following reasons (among others)
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.
rc = checkAndLoadTable(caslib, name);
if ( rc ne true) then do;
... handle error...
end;
else do;
... real work ...
end;
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');
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Data Literacy is for all, even absolute beginners. Jump on board with this free e-learning and boost your career prospects.