Published on :
Macro CREATION_INTERNE

Macro to check for dataset existence

This code is also available in: Français Deutsch Español
Awaiting validation
The purpose of this macro is to provide a reliable method for testing the existence of a SAS© dataset. It accepts the dataset name as a positional parameter. The internal logic first isolates the library and member name. Special processing is applied for the SASHELP library, where only a predefined list of dictionary views is considered to exist. For a library specifically named 'DB', the macro consistently returns 1 (true), acting as a workaround for virtual datasets. For all other cases, it constructs the physical path and uses functions and macros (%sysfunc(pathname), %_exist) to check for the file's presence on the system, testing for possible SAS© file suffixes (e.g., .sas©7bdat, .sastbvw).
Data Analysis

Type : CREATION_INTERNE


The macro itself does not read or create data; it verifies their existence. The validation block, provided in a comment, creates temporary test datasets (`WORK.DATA_n`, `PWD.TEST2`) to verify the macro's proper functioning.

1 Code Block
MACRO
Explanation :
Definition of the _dsexist macro. It takes a dataset name as an argument. It extracts the library and table name, then applies conditional logic: specific processing for dictionary views of the SASHELP library, a fixed return value for a 'DB' library, and a check for the physical file's existence for all other libraries.
Copied!
1%macro _dsexist(arg1, DATA=&arg1);
2 
3%local lib;
4 
5%let lib=%_lib(&DATA);
6%let DATA=%_data(&DATA);
7%*put LIB=&lib;
8%IF &lib=sashelp %THEN %DO;
9 %IF &DATA=vcatalg | &DATA=vcolumn | &DATA=vextfl | &DATA=vindex |
10 &DATA=vmacro | &DATA=vmember | &DATA=voption | &DATA=vtable |
11 &DATA=vtitle | &DATA=vview | &DATA=vsacces | &DATA=vscatlg |
12 &DATA=vslib | &DATA=vstable | &DATA=vstabvw | &DATA=vsview %THEN 1;
13 %ELSE 0;
14%END;
15%ELSE %IF &lib=db %THEN 1;
16%ELSE %DO;
17 %local suffix1 suffix2;
18 %let suffix1=%_suffix;
19 %let suffix2=%scan(&suffix1, 3, %str( ));
20 %let suffix1=%scan(&suffix1, 1, %str( ));
21 %let DATA=%_dir(%sysfunc(pathname(&lib)))&DATA;
22
23 %IF %_exist(&DATA..&suffix1) %THEN 1;
24 %ELSE %IF %LENGTH(&suffix2) & %_exist(&DATA..&suffix2) %THEN 1;
25 %ELSE 0;
26%END;
27 
28%mend _dsexist;
2 Code Block
DATA STEP Data
Explanation :
Part of the validation flow (commented out). This block first assigns the 'PWD' library to the current directory. Then, it creates an empty dataset in the WORK library (automatically named data1, data2, etc.) and another empty dataset named 'test2' in the 'PWD' library.
Copied!
1/* uncomment to re-validate
2 
3LIBNAME pwd '.';
4 
5DATA;
6RUN;
7 
8DATA pwd.test2;
9RUN;
3 Code Block
MACRO CALL
Explanation :
Part of the validation flow (commented out). This block executes a series of tests by calling the %_dsexist macro with different types of inputs (null, existing, non-existent datasets, in SASHELP, etc.) to verify that the returned result is correct. Results are displayed in the SAS log via %PUT.
Copied!
1%put ATTN: %_dsexist(_null_);
2%put ATTN: %_dsexist(data1);
3%put ATTN: %_dsexist(sashelp.voption);
4%put ATTN: %_dsexist(sashelp.voption(obs=1.));
5%put ATTN: %_dsexist(sashelp.option);
6%put ATTN: %_dsexist(work.data1);
7%put ATTN: %_dsexist(work.data2);
8%put ATTN: %_dsexist(pwd.test1);
9%put ATTN: %_dsexist(pwd.test2);
10%put ATTN: %_exist(test2.s*);
4 Code Block
MACRO CALL
Explanation :
Last step of the validation flow (commented out). This call to an (unprovided) %_delete macro is intended to clean up the test environment by deleting the 'pwd.test2' dataset created previously.
Copied!
1%_delete(DATA=pwd.test2);
2 
3*/
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 : Copyright (c) 2001-2025 Rodney Sparapani