Production Ready Macros for SAS Application Developers
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> Dependencies </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 **/
18 
19 %macro mf_existvarlist(libds, varlist
20 )/*/STORE SOURCE*/;
21 
22  %if %str(&libds)=%str() or %str(&varlist)=%str() %then %do;
23  %mf_abort(msg=No value provided to libds(&libds) or varlist (&varlist)!
24  ,mac=mf_existvarlist.sas)
25  %end;
26 
27  %local dsid rc i var found;
28  %let dsid=%sysfunc(open(&libds,is));
29 
30  %if &dsid=0 %then %do;
31  %put WARNING: unable to open &libds in mf_existvarlist (&dsid);
32  %end;
33 
34  %if %sysfunc(attrn(&dsid,NVARS))=0 %then %do;
35  %put MF_EXISTVARLIST: No variables in &libds ;
36  0
37  %return;
38  %end;
39 
40  %else %do i=1 %to %sysfunc(countw(&varlist));
41  %let var=%scan(&varlist,&i);
42 
43  %if %sysfunc(varnum(&dsid,&var))=0 %then %do;
44  %let found=&found &var;
45  %end;
46  %end;
47 
48  %let rc=%sysfunc(close(&dsid));
49  %if %str(&found)=%str() %then %do;
50  1
51  %end;
52  %else %do;
53  0
54  %put Vars not found: &found;
55  %end;
56 %mend;