Macros for SAS Application Developers
https://github.com/sasjs/core
mf_existvar.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Checks if a variable exists in a data set.
4  @details Returns 0 if the variable does NOT exist, and the position of the var
5  if it does.
6  Usage:
7 
8  %put %mf_existvar(work.someds, somevar)
9 
10  @param [in] libds 2 part dataset or view reference
11  @param [in] var variable name
12 
13  <h4> Related Macros </h4>
14  @li mf_existvar.test.sas
15 
16  @version 9.2
17  @author Allan Bowe
18 **/
19 /** @cond */
20 
21 %macro mf_existvar(libds /* 2 part dataset name */
22  , var /* variable name */
23 )/*/STORE SOURCE*/;
24 
25  %local dsid rc;
26  %let dsid=%sysfunc(open(&libds,is));
27 
28  %if &dsid=0 %then %do;
29  %put %sysfunc(sysmsg());
30  0
31  %end;
32  %else %if %length(&var)=0 %then %do;
33  0
34  %let rc=%sysfunc(close(&dsid));
35  %end;
36  %else %do;
37  %sysfunc(varnum(&dsid,&var))
38  %let rc=%sysfunc(close(&dsid));
39  %end;
40 
41 %mend mf_existvar;
42 
43 /** @endcond */