forked from sasutils/macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentv.sas
177 lines (155 loc) · 6.02 KB
/
contentv.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
%macro contentv
/*----------------------------------------------------------------------
Creates VIEW or DATASET containing the data file column attributes.
----------------------------------------------------------------------*/
(data /* data set name */
,libname= /* library reference */
,memname= /* member name list to include */
,memnamex= /* member name list to exclude */
,memtype=DATA VIEW /* member types */
,view=_CONTENT /* output view name */
,out= /* output table name */
,genfmt= /* Generate FORMAT when null? (default=yes) */
);
/*----------------------------------------------------------------------
Based on original macro created by Tom Hoffman.
-----------------------------------------------------------------------
Usage:
1) %contentv;
Creates view _CONTENT of the last created data file.
2) %contentv(EVENTS)
Creates view _CONTENT of the data file WORK.EVENTS.
4) %contentv(DB.LABS)
Creates view _CONTENT of the data file DB.LABS.
5) %contentv(libname=SASUSER)
Creates view _CONTENT containing variable attributes of all
data files in the SASUSER library.
6) %contentv(libname=SASUSER,memname=houses mydata)
Creates view _CONTENT containing variable attributes of all
two members in the SASUSER library.
------------------------------------------------------------------------
Notes:
Either the data parameter or the libname/memname parameter may be used.
Creates VIEW containing the data file contents. The view contains the
the following variables (from DICTIONARY.COLUMNS):
LIBNAME $8 Library Name
MEMNAME $32 Member Name - upcased
MEMTYPE $8 Member Type
NAME $32 Column Name - upcased
TYPE $4 Column Type (char or num)
LENGTH 8 Column Length
NPOS 8 Column Position
VARNUM 8 Column Number in Table
LABEL $40 Column Label
FORMAT $16 Column Format
INFORMAT $16 Column Informat
IDXUSAGE $9 Column Index Type
where if null the format variable has been set to BEST12. for numeric
variables and to the length for character variables.
-----------------------------------------------------------------------
History:
04OCT95 TRHoffman Creation
04FEB99 TRHoffman Added MEMNAMEX parameter.
27SEP00 TRHoffman Implemented changes required by V8
26FEB02 TRHoffman Added support for lower case table names.
16JUN07 abernt Added support of long character variables
17FEB08 abernt Added support for multiple libnames.
Added test for view/dataset conflict on output.
Made format generation when null optional.
----------------------------------------------------------------------*/
%*----------------------------------------------------------------------
Test if request to generate format when null. Support aliases.
-----------------------------------------------------------------------;
%if ^%length(&genfmt) %then %let genfmt=1;
%else %if %sysfunc(indexw(1 Y T ON YES TRUE,%qupcase(&genfmt)))
%then %let genfmt=1;
%else %let genfmt=0;
%*----------------------------------------------------------------------
Parse data parameter for LIBNAME and MEMNAME. Support where clause.
Support MEMNAMEX for case when memname=_ALL_.
-----------------------------------------------------------------------;
%if ("&data" ^= "") %then %do;
%let data = %upcase(&data);
%let memname = %scan(&data,2,.);
%if (&memname =) %then %do;
%let libname = WORK;
%let memname = %scan(&data,1,%str(%());
%end;
%else %do;
%let libname = %scan(&data,1,.);
%let memname = %scan(&memname,1,%str(%());
%end;
%let memnamex=%upcase(&memnamex);
%end;
%*----------------------------------------------------------------------
Assume last created data set.
-----------------------------------------------------------------------;
%else %if ^%length(&libname) %then %do;
%let libname = %scan(&sysdsn,1);
%let memname = %substr(&sysdsn,9);
%let memnamex=;
%end;
%*----------------------------------------------------------------------
Use values of LIBNAME and MEMNAME
-----------------------------------------------------------------------;
%else %do;
%let libname = %upcase(&libname);
%let memname = %upcase(&memname);
%let memnamex = %upcase(&memnamex);
%end;
%*----------------------------------------------------------------------
Create VIEW from DICTIONARY.COLUMNS. Note that this SQL view is about 25
times faster than the SASHELP vcolumn view.
Generate FORMAT when null.
-----------------------------------------------------------------------;
proc sql;
%if %length(&out) and %sysfunc(exist(&out,view)) %then %do;
drop view &out;
%end;
%else %if %sysfunc(exist(&view,data)) %then %do;
drop table &view;
%end;
%if %length(&out) %then
create table &out as
;
%else
create view &view as
;
select libname /* $8. library name */
, upcase(memname) as memname/* $32 member name */
, memtype /* $8 member type */
, upcase(name) as name label='Column Name'
, type /* $4. column type */
, length /* best12. column length */
, npos /* best12. column position */
, varnum /* best12. column number in table */
, label /* $40. column label */
,
%if (&genfmt) %then
case when format is null then
case (type)
when 'char' then compress('$'||put(length,5.)||'.')
else 'best12.'
end
else format
end
;
%else format
;
as format label='Column Format'
, informat /* $16. column informat */
, idxusage /* $9. column index type */
from dictionary.columns
where libname in %qlist(&libname)
%if %length(&memtype) %then
and memtype in %qlist(%upcase(&memtype))
;
%if %length(&memname) & (&memname ^= _ALL_) %then
and upcase(memname) in %qlist(&memname)
;
%if %length(&memnamex) %then
and upcase(memname) ^in %qlist(&memnamex)
;
;
quit;
%mend contentv;