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