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 [in] libds Two part dataset (or view) reference.
17  @param [in] 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 %do;
43  %put &sysmacroname: dataset &libds not opened! (rc=&dsid);
44  %put &sysmacroname: %sysfunc(sysmsg());
45  %return;
46  %end;
47 
48  /* Close dataset */
49  %let rc = %sysfunc(close(&dsid));
50  /* Return variable type */
51  &vtype
52 %mend mf_getvartype;