-
Notifications
You must be signed in to change notification settings - Fork 8
/
lengthcheck.sas
137 lines (111 loc) · 4.48 KB
/
lengthcheck.sas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
%macro LengthCheck(ds,vars,out=lengths,fmtlen=12) / des='Output the max length for variables';
/********************************************************************************
BEGIN MACRO HEADER
********************************************************************************
Name: LengthCheck
Author: Chris Swenson
Created: 2011-09-23
Purpose: Output the maximum length for variables
Arguments: ds - input data set
vars - input variable(s), can use _ALL_ to specify all vars
out= - output data set, defaulted to Lengths
fmtlen= - format length of the output length column, defaulted
to 12
Dependency: VarInfo - outputs information about variable, in this instance,
the variable type (char, num)
Revisions
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Date Author Comments
¯¯¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯
YYYY-MM-DD III Please use this format and insert new entries above
********************************************************************************
END MACRO HEADER
********************************************************************************/
/* Check for blank arguments */
%if %superq(DS)=%str() %then %do;
%put %str(E)RROR: No argument specified for DS.;
%return;
%end;
%if %superq(VARS)=%str() %then %do;
%put %str(E)RROR: No argument specified for VARS.;
%return;
%end;
%if %superq(FMTLEN)=%str() %then %do;
%put %str(E)RROR: No argument specified for FMTLEN.;
%return;
%end;
/* Check that the data set has records */
%local ok;
%let ok=0;
data _null_;
set &DS(obs=1);
if _n_=1 then call symputx('ok', '1');
run;
%if &OK=0 %then %do;
%put %str(E)RROR: The data set &DS is empty.;
%return;
%end;
/* Check for numeric values */
%if %sysfunc(compress(&FMTLEN, %str(), %str(d))) ne %str() %then %do;
%put %str(E)RROR: %str(I)nvalid argument specified for FMTLEN.;
%put %str(E)RROR: Please use numeric values only.;
%return;
%end;
/* Check for existing data */
%if %sysfunc(exist(&OUT)) %then %do;
%put %str(W)ARNING: The output data set &OUT already exists.;
%return;
%end;
/* If ALL is specified, output all of the variables */
%if %upcase(&VARS)=_ALL_ %then %do;
proc contents data=&DS out=_contents_(keep=name varnum) noprint;
proc sort data=_contents_;
by varnum;
run;
proc sql noprint;
select name
into :vars separated by ' '
from _contents_
;
drop table _contents_;
quit;
%end;
%local count i var type;
%let count=%sysfunc(countw(&VARS, %str( )));
%do i=1 %to &COUNT;
%let var=%scan(&VARS, &I, %str( ));
%let type=%VarInfo(&DS, &VAR, type);
%let fmt=%VarInfo(&DS, &VAR, format);
proc sql;
create table _temp_ as
select "&VAR" as Column length=32
%if &TYPE=C %then %do;
, min(length(&VAR)) as Length_Min length=8 format=comma&FMTLEN..
, max(length(&VAR)) as Length length=8 format=comma&FMTLEN..
, . as Max length=8 format=comma&FMTLEN..
%end;
%else %do;
, min(length(strip(put(&VAR, &FMT.)))) as Length_Min length=8 format=comma&FMTLEN..
, max(length(strip(put(&VAR, &FMT.)))) as Length length=8 format=comma&FMTLEN..
, max(&VAR) as Max length=8 format=comma&FMTLEN..
%end;
from &DS
;
quit;
proc append base=&OUT data=_temp_;
run;
proc sql;
drop table _temp_;
quit;
%end;
data _null_;
set &OUT;
if _n_=1 then do;
put @35 'Length' @50 'Length' @65 'Max';
put 'Column' @35 '(min)' @50 '(max)' @65 'Value';
put '------' @35 '------' @50 '------' @65 '-----';
end;
if missing(max) then put column @35 length_min @50 length @65 'n/a';
else put column @35 length_min @50 length @65 max;
run;
%mend LengthCheck;