Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
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, if the dataset
5 cannot be opened, if it holds no variables, or if `libds` / `varlist` is
6 blank. Returns 1 if they ALL exist. The macro never aborts: a blank input
7 answers 0 and the calling program decides whether that is a condition to
8 stop on (`mp_abort`) or simply a negative result.
9 Usage:
10
11 %put %mf_existVarList(sashelp.class, age sex name dummyvar);
12
13 @param [in] libds 2 part dataset or view reference
14 @param [in] varlist space separated variable names
15
16 @version 9.2
17 @author Allan Bowe
18 @cond
19**/
20
21%macro mf_existvarlist(libds, varlist
22)/*/STORE SOURCE*/;
23
24 /* nothing to check - answer 0 and let the caller decide whether a blank
25 input is a condition worth stopping on */
26 %if %str(&libds)=%str() or %str(&varlist)=%str() %then %do;
27 %put %str(WARN)ING: blank libds/varlist in mf_existvarlist;
28 0
29 %return;
30 %end;
31
32 %local dsid rc i var found;
33 %let dsid=%sysfunc(open(&libds,is));
34
35 %if &dsid=0 %then %do;
36 %put %str(WARN)ING: unable to open &libds in mf_existvarlist (&dsid);
37 /* the failed open reports through SYSMSG, not SYSERR or SYSCC, so the
38 return value is the whole signal - and ATTRN and VARNUM return a missing
39 value for dsid 0, which would report a match, so return before probing */
40 0
41 %return;
42 %end;
43
44 %if %sysfunc(attrn(&dsid,NVARS))=0 %then %do;
45 %put MF_EXISTVARLIST: No variables in &libds ;
46 %let rc=%sysfunc(close(&dsid));
47 0
48 %return;
49 %end;
50
51 %else %do i=1 %to %sysfunc(countw(&varlist));
52 %let var=%scan(&varlist,&i);
53
54 %if %sysfunc(varnum(&dsid,&var))=0 %then %do;
55 %let found=&found &var;
56 %end;
57 %end;
58
59 %let rc=%sysfunc(close(&dsid));
60 %if %str(&found)=%str() %then %do;
61 1
62 %end;
63 %else %do;
64 0
65 %put Vars not found: &found;
66 %end;
67%mend mf_existvarlist;
68
69/** @endcond */