Macros for SAS Application Developers
https://github.com/sasjs/core
mf_existvarlist.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Checks if a set of variables ALL exist in a data set.
4  @details Returns 0 if ANY of the variables do not exist, or 1 if they ALL do.
5  Usage:
6 
7  %put %mf_existVarList(sashelp.class, age sex name dummyvar);
8 
9  @param [in] libds 2 part dataset or view reference
10  @param [in] varlist space separated variable names
11 
12  @version 9.2
13  @author Allan Bowe
14  @cond
15 **/
16 
17 %macro mf_existvarlist(libds, varlist
18 )/*/STORE SOURCE*/;
19 
20  %if %str(&libds)=%str() or %str(&varlist)=%str() %then %do;
21  %mf_abort(msg=No value provided to libds(&libds) or varlist (&varlist)!
22  ,mac=mf_existvarlist.sas)
23  %end;
24 
25  %local dsid rc i var found;
26  %let dsid=%sysfunc(open(&libds,is));
27 
28  %if &dsid=0 %then %do;
29  %put %str(WARN)ING: unable to open &libds in mf_existvarlist (&dsid);
30  %end;
31 
32  %if %sysfunc(attrn(&dsid,NVARS))=0 %then %do;
33  %put MF_EXISTVARLIST: No variables in &libds ;
34  0
35  %return;
36  %end;
37 
38  %else %do i=1 %to %sysfunc(countw(&varlist));
39  %let var=%scan(&varlist,&i);
40 
41  %if %sysfunc(varnum(&dsid,&var))=0 %then %do;
42  %let found=&found &var;
43  %end;
44  %end;
45 
46  %let rc=%sysfunc(close(&dsid));
47  %if %str(&found)=%str() %then %do;
48  1
49  %end;
50  %else %do;
51  0
52  %put Vars not found: &found;
53  %end;
54 %mend mf_existvarlist;
55 
56 /** @endcond */