Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mf_getvartype.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Returns variable type - Character (C) or Numeric (N)
4  @details
5 Usage:
6 
7  data test;
8  length str $1. num 8.;
9  stop;
10  run;
11  %put %mf_getvartype(test,str);
12  %put %mf_getvartype(work.test,num);
13 
14 
15 
16  @param libds Two part dataset (or view) reference.
17  @param var the variable name to be checked
18  @return output returns C or N depending on variable type. If variable
19  does not exist then a blank is returned and a note is written to the log.
20 
21  @version 9.2
22  @author Allan Bowe
23 
24 **/
25 
26 %macro mf_getvartype(libds /* two level name */
27  , var /* variable name from which to return the type */
28 )/*/STORE SOURCE*/;
29  %local dsid vnum vtype rc;
30  /* Open dataset */
31  %let dsid = %sysfunc(open(&libds));
32  %if &dsid. > 0 %then %do;
33  /* Get variable number */
34  %let vnum = %sysfunc(varnum(&dsid, &var));
35  /* Get variable type (C/N) */
36  %if(&vnum. > 0) %then %let vtype = %sysfunc(vartype(&dsid, &vnum.));
37  %else %do;
38  %put NOTE: Variable &var does not exist in &libds;
39  %let vtype = %str( );
40  %end;
41  %end;
42  %else %put dataset &libds not opened! (rc=&dsid);
43 
44  /* Close dataset */
45  %let rc = %sysfunc(close(&dsid));
46  /* Return variable type */
47  &vtype
48 %mend;