Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mf_getvarnum.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns the position of a variable in dataset (varnum attribute).
4  @details Uses varnum function to determine position.
5 
6 Usage:
7 
8  data work.test;
9  format str $1. num datetime19.;
10  stop;
11  run;
12  %put %mf_getVarNum(work.test,str);
13  %put %mf_getVarNum(work.test,num);
14  %put %mf_getVarNum(work.test,renegade);
15 
16 returns:
17 
18  > 1
19 
20  > 2
21 
22  > NOTE: Variable renegade does not exist in test
23 
24  @param libds Two part dataset (or view) reference.
25  @param var Variable name for which a position should be returned
26 
27  @author Allan Bowe
28  @version 9.2
29 
30 **/
31 
32 %macro mf_getVarNum(libds /* two level ds name */
33  , var /* variable name from which to return the format */
34 )/*/STORE SOURCE*/;
35  %local dsid vnum rc;
36  /* Open dataset */
37  %let dsid = %sysfunc(open(&libds));
38  %if &dsid > 0 %then %do;
39  /* Get variable number */
40  %let vnum = %sysfunc(varnum(&dsid, &var));
41  %if(&vnum <= 0) %then %do;
42  %put NOTE: Variable &var does not exist in &libds;
43  %let vnum = %str( );
44  %end;
45  %end;
46  %else %put dataset &ds not opened! (rc=&dsid);
47 
48  /* Close dataset */
49  %let rc = %sysfunc(close(&dsid));
50 
51  /* Return variable number */
52  &vnum.
53 
54 %mend;