Production Ready 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  <h4> SAS Macros </h4>
10  @li mf_abort.sas
11 
12  @param libds 2 part dataset or view reference
13  @param varlist space separated variable names
14 
15  @version 9.2
16  @author Allan Bowe
17  @cond
18 **/
19 
20 %macro mf_existvarlist(libds, varlist
21 )/*/STORE SOURCE*/;
22 
23  %if %str(&libds)=%str() or %str(&varlist)=%str() %then %do;
24  %mf_abort(msg=No value provided to libds(&libds) or varlist (&varlist)!
25  ,mac=mf_existvarlist.sas)
26  %end;
27 
28  %local dsid rc i var found;
29  %let dsid=%sysfunc(open(&libds,is));
30 
31  %if &dsid=0 %then %do;
32  %put WARNING: unable to open &libds in mf_existvarlist (&dsid);
33  %end;
34 
35  %if %sysfunc(attrn(&dsid,NVARS))=0 %then %do;
36  %put MF_EXISTVARLIST: No variables in &libds ;
37  0
38  %return;
39  %end;
40 
41  %else %do i=1 %to %sysfunc(countw(&varlist));
42  %let var=%scan(&varlist,&i);
43 
44  %if %sysfunc(varnum(&dsid,&var))=0 %then %do;
45  %let found=&found &var;
46  %end;
47  %end;
48 
49  %let rc=%sysfunc(close(&dsid));
50  %if %str(&found)=%str() %then %do;
51  1
52  %end;
53  %else %do;
54  0
55  %put Vars not found: &found;
56  %end;
57 %mend;
58 
59 /** @endcond */