Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mfv_existsashdat.sas
Go to the documentation of this file.
1/**
2 @file mfv_existsashdat.sas
3 @brief Checks whether a CAS sashdat dataset exists in persistent storage.
4 @details Can be used in open code, eg as follows:
5
6 %if %mfv_existsashdat(libds=casuser.sometable) %then %put yes it does!;
7
8 The function uses `dosubl()` to run the `table.fileinfo` action, for the
9 specified library, filtering for `*.sashdat` tables.
10
11 Results are cached in a WORK table (&outprefix._&lib). If that table
12 already exists it is queried directly to avoid the dosubl() overhead.
13 To force a rescan, use a new `&outprefix` value or delete the cache
14 table before calling.
15
16 @param [in] libds library.dataset
17 @param [out] outprefix= (work.mfv_existsashdat)
18 Used to store current HDATA tables to improve subsequent query performance.
19 This reference is a prefix and is converted to `&prefix._{libref}`
20
21 @return output returns 1 or 0
22
23 @version 0.2
24 @author Mathieu Blauw
25**/
26
27%macro mfv_existsashdat(libds,outprefix=work.mfv_existsashdat);
28%local rc dsid name lib ds;
29%let lib=%upcase(%scan(&libds,1,'.'));
30%let ds=%upcase(%scan(&libds,-1,'.'));
31
32/* if cache table does not exist, build it */
33%if %sysfunc(exist(&outprefix._&lib)) ne 1 %then %do;
34 %let rc=%sysfunc(dosubl(%nrstr(
35 /* Read in table list (once per &lib per session) */
36 proc cas;
37 table.fileinfo result=source_list /caslib="&lib";
38 val=findtable(source_list);
39 saveresult val dataout=&outprefix._&lib;
40 quit;
41 /* Only keep name, without file extension */
42 data &outprefix._&lib;
43 set &outprefix._&lib(where=(upcase(Name) like '%.SASHDAT') keep=Name);
44 Name=upcase(scan(Name,1,'.'));
45 run;
46 )));
47%end;
48
49/* Scan table for hdat existence */
50%let dsid=%sysfunc(open(&outprefix._&lib(where=(name="&ds"))));
51%syscall set(dsid);
52%let rc = %sysfunc(fetch(&dsid));
53%let rc = %sysfunc(close(&dsid));
54
55/* Return result */
56%if "%trim(&name)"="%trim(&ds)" %then 1;
57%else 0;
58
59%mend mfv_existsashdat;