Production Ready 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 return the position of
5  the var if it does.
6  Usage:
7 
8  %put %mf_existvar(work.someds, somevar)
9 
10  @param libds (positional) - 2 part dataset or view reference
11  @param var (positional) - variable name
12  @version 9.2
13  @author Allan Bowe
14 **/
15 /** @cond */
16 
17 %macro mf_existvar(libds /* 2 part dataset name */
18  , var /* variable name */
19 )/*/STORE SOURCE*/;
20 
21  %local dsid rc;
22  %let dsid=%sysfunc(open(&libds,is));
23 
24  %if &dsid=0 or %length(&var)=0 %then %do;
25  %put %sysfunc(sysmsg());
26  0
27  %end;
28  %else %do;
29  %sysfunc(varnum(&dsid,&var))
30  %let rc=%sysfunc(close(&dsid));
31  %end;
32 
33 %mend;
34 
35 /** @endcond */