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!
%if %sysfunc(exist(&outprefix._&lib)) ne 1 %then %do;
%let rc=%sysfunc(dosubl(%nrstr(
/* Read in table list (once per &lib per session) */
proc cas;
table.fileinfo result=source_list /caslib="&lib";
val=findtable(source_list);
saveresult val dataout=&outprefix._&lib;
quit;
/* Only keep name, without file extension */
data &outprefix._&lib;
set &outprefix._&lib(where=(Name like '%.sashdat') keep=Name);
Name=upcase(scan(Name,1,'.'));
run;
)));
%end;
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!
/* Scan table for hdat existence */
%let dsid=%sysfunc(open(&outprefix._&lib(where=(name="&ds"))));
%syscall set(dsid);
%let rc = %sysfunc(fetch(&dsid));
%let rc = %sysfunc(close(&dsid));
/* Return result */
%if "%trim(&name)"="%trim(&ds)" %then 1;
%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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.