Published on :
Macro EXTERNE

Checking for SASHDAT file existence in CAS

This code is also available in: Deutsch Español Français
Awaiting validation
This macro checks for the existence of a `.sashdat` file in a CASLIB. It uses the CAS action `table.fileinfo` executed via `dosubl()` to retrieve the list of files. To optimize performance during multiple calls, the result is cached in a local WORK table (defined by `outprefix`). The macro returns 1 if the file exists, 0 otherwise.
Data Analysis

Type : EXTERNE


Data comes from the CAS server's system information (list of files in a CASLIB) retrieved via `PROC CAS`.

1 Code Block
PROC CAS Data
Explanation :
This conditional block checks if the cache exists. If not, it uses `dosubl` to execute `PROC CAS` and the `table.fileinfo` action to list the library's files, then a DATA step to filter only `.sashdat` files and create the cache table.
Copied!
1%IF %sysfunc(exist(&outprefix._&lib)) ne 1 %THEN %DO;
2 %let rc=%sysfunc(dosubl(%nrstr(
3 /* Read in table list (once per &lib per session) */
4 PROC CAS;
5 TABLE.fileinfo RESULT=source_list /caslib="&lib";
6 val=findtable(source_list);
7 saveresult val dataout=&outprefix._&lib;
8 QUIT;
9 /* Only keep name, without file extension */
10 DATA &outprefix._&lib;
11 SET &outprefix._&lib(where=(Name like '%.sashdat') keep=Name);
12 Name=upcase(scan(Name,1,'.'));
13 RUN;
14 )));
15%END;
2 Code Block
SAS Functions
Explanation :
This block queries the local cache table via `open` and `fetch` functions to check if the requested table name (`&ds`) is present, and returns the boolean result (1 or 0).
Copied!
1/* Scan table for hdat existence */
2%let dsid=%sysfunc(open(&outprefix._&lib(where=(name="&ds"))));
3%syscall SET(dsid);
4%let rc = %sysfunc(fetch(&dsid));
5%let rc = %sysfunc(close(&dsid));
6 
7/* Return result */
8%IF "%trim(&name)"="%trim(&ds)" %THEN 1;
9%ELSE 0;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Mathieu Blauw