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
6Usage:
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
16returns:
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 %do;
47 %put &sysmacroname: dataset &libds not opened! (rc=&dsid);
48 %put &sysmacroname: %sysfunc(sysmsg());
49 %return;
50 %end;
51
52 /* Close dataset */
53 %let rc = %sysfunc(close(&dsid));
54
55 /* Return variable number */
56 &vnum.
57
58%mend mf_getVarNum;