Production Ready Macros for SAS Application Developers
mp_getmaxvarlengths.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Scans a dataset to find the max length of the variable values
4  @details
5  This macro will scan a base dataset and produce an output dataset with two
6  columns:
7 
8  - COL Name of the base dataset column
9  - MAXLEN Maximum length of the data contained therein.
10 
11  Character fields may be allocated very large widths (eg 32000) of which the maximum
12  value is likely to be much narrower. This macro was designed to enable a HTML
13  table to be appropriately sized however this could be used as part of a data
14  audit to ensure we aren't over-sizing our tables in relation to the data therein.
15 
16  Numeric fields are converted using the relevant format to determine the width.
17  Usage:
18 
19  %mp_getmaxvarlengths(sashelp.class,outds=work.myds);
20 
21  @param libds Two part dataset (or view) reference.
22  @param outds= The output dataset to create
23 
24  @version 9.2
25  @author Allan Bowe
26 
27 **/
28 
29 %macro mp_getmaxvarlengths(
30  libds /* libref.dataset to analyse */
31  ,outds=work.mp_getmaxvarlengths /* name of output dataset to create */
32 )/*/STORE SOURCE*/;
33 
34 %local vars x var fmt;
35 %let vars=%getvars(libds=&libds);
36 
37 proc sql;
38 create table &outds (rename=(
39  %do x=1 %to %sysfunc(countw(&vars,%str( )));
40  _&x=%scan(&vars,&x)
41  %end;
42  ))
43  as select
44  %do x=1 %to %sysfunc(countw(&vars,%str( )));
45  %let var=%scan(&vars,&x);
46  %if &x>1 %then ,;
47  %if %mf_getvartype(&libds,&var)=C %then %do;
48  max(length(&var)) as _&x
49  %end;
50  %else %do;
51  %let fmt=%mf_getvarformat(&libds,&var);
52  %put fmt=&fmt;
53  %if %str(&fmt)=%str() %then %do;
54  max(length(cats(&var))) as _&x
55  %end;
56  %else %do;
57  max(length(put(&var,&fmt))) as _&x
58  %end;
59  %end;
60  %end;
61  from &libds;
62 
63  proc transpose data=&outds
64  out=&outds(rename=(_name_=name COL1=ACTMAXLEN));
65  run;
66 
67 %mend;