Voici deux programmes permettant de créer une table dont les longueurs de champs sont optimisées selon les valeurs contenues.

Usage :
Code :Sélectionner tout -Visualiser dans une fenêtre à part
1
2
3
4
5
%OPTIMUM(
                  DSNIN=sashelp.class
                , DSNOUT=work.class
                , COMPRESS=BINARY
                ) ;


le code (1) :
Code :Sélectionner tout -Visualiser dans une fenêtre à part
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
%macro OPTIMUM(   DSNIN
			, DSNOUT
			, VARignoree=
			, COMPRESS=
			);
	%let _notes=%sysfunc(getoption(notes,keyword));
	%let _spool=%sysfunc(getoption(spool,keyword));
	%let _source=%sysfunc(getoption(source,keyword));
	%let _symbolgen=%sysfunc(getoption(symbolgen,keyword));
	%let _mprint=%sysfunc(getoption(mprint,keyword));
	%let _mfile=%sysfunc(getoption(mfile,keyword));
	options nospool nosource nosymbolgen nomprint nonotes;
	%global OPTIMUM;
	%local I;
 
	%if "&DSNIN" = "&DSNOUT" %then
		%do;
			%let etape=  1;
			%goto erreur;
		%end;
 
	%put =============================================================;
	%put Macro optimum exécutée sur &dsnin;
	%put =============================================================;
 
	proc contents data=&DSNIN( drop=&VARignoree ) memtype=data noprint out=_cntnts_( keep= name type );
	run;
 
	%if &syserr > 1 %then
		%do;
			%let etape=  1;
			%goto erreur;
		%end;
 
	%let N_CHAR = 0;
	%let N_NUM  = 0;
 
data _null_;
	set _cntnts_ end=lastobs nobs=nobs;
 
	if nobs = 0 then
		stop;
	n_char + ( type = 2 );
	n_num  + ( type = 1 );
 
	if lastobs then
		do;
			call symput( 'N_CHAR', left( put( n_char, 5. )));
			call symput( 'N_NUM' , left( put( n_num , 5. )));
		end;
run;
 
%if &syserr > 1 %then
	%do;
		%let etape=  2;
		%goto erreur;
	%end;
 
%if %eval( &N_NUM + &N_CHAR ) = 0 %then
	%do;
		%put pas de variables dans la table;
		%goto erreur;
	%end;
 
%do I = 1 %to &N_NUM;
	%global NUM&I NUMLEN&I;
%end;
 
%do I = 1 %to &N_CHAR;
	%global CHAR&I CHARLEN&I;
%end;
 
proc sql noprint;
	%if &N_CHAR > 0 %then
		%str( select name into :CHAR1 - :CHAR&N_CHAR from _cntnts_ where type = 2 ; );
 
	%if &N_NUM  > 0 %then
		%str( select name into :NUM1  - :NUM&N_NUM   from _cntnts_ where type = 1 ; );
quit;
 
%if &syserr > 1 %then
	%do;
		%let etape=  3;
		%goto erreur;
	%end;
 
data _null_;
	set &DSNIN end=lastobs;
 
	%if &N_NUM  > 0 %then
		%str ( array _num_len_  ( &N_NUM  ) 3 _temporary_ ; );
 
	%if &N_CHAR > 0 %then
		%str( array _char_len_ ( &N_CHAR ) _temporary_ ; );
 
	if _n_ = 1 then
		do;
			%if &N_CHAR > 0 %then %str(
 
				do i = 1 to &N_CHAR ; _char_len_( i ) = 0 ; end ; );
					%if &N_NUM  > 0 %then %str(
 
						do i = 1 to &N_NUM  ; _num_len_ ( i ) = 3 ; end ; );
						end;
 
					%if &N_CHAR > 0 %then
						%do;
							%do I = 1 %to &N_CHAR;
								_char_len_( &I ) = max( _char_len_( &I ), length( &&CHAR&I ));
							%end;
						%end;
 
					%if &N_NUM > 0 %then
						%do I = 1 %to &N_NUM;
							if &&NUM&I ne . then
								do;
									if &&NUM&I ne trunc( &&NUM&I, 7 ) then
										_num_len_( &I ) = max( _num_len_( &I ), 8 );
									else if &&NUM&I ne trunc( &&NUM&I, 6 ) then
										_num_len_( &I ) = max( _num_len_( &I ), 7 );
									else if &&NUM&I ne trunc( &&NUM&I, 5 ) then
										_num_len_( &I ) = max( _num_len_( &I ), 6 );
									else if &&NUM&I ne trunc( &&NUM&I, 4 ) then
										_num_len_( &I ) = max( _num_len_( &I ), 5 );
									else if &&NUM&I ne trunc( &&NUM&I, 3 ) then
										_num_len_( &I ) = max( _num_len_( &I ), 4 );
								end;
						%end;
 
					if lastobs then
						do;
							%if &N_CHAR > 0 %then
								%do;
									%do I = 1 %to &N_CHAR;
										call symput( "CHARLEN&I", put( _char_len_( &I ), 5. ));
									%end;
								%end;
 
							%if &N_NUM > 0 %then
								%do I = 1 %to &N_NUM;
									call symput( "NUMLEN&I", put( _num_len_( &I ), 1. ));
								%end;
						end;
run;
 
%if &syserr > 1 %then
	%do;
		%let etape=  4;
		%goto erreur;
	%end;
 
proc datasets nolist;
	delete _cntnts_;
run;
 
%if &syserr > 1 %then
	%do;
		%let etape=  5;
		%goto erreur;
	%end;
 
%let SQZ_NUM      = LENGTH;
%let SQZ_CHAR     = LENGTH;
%let SQZ_CHAR_FMT = FORMAT;
 
%if &N_CHAR > 0 %then
	%do I = 1 %to &N_CHAR;
		%let SQZ_CHAR     = &SQZ_CHAR %qtrim( &&CHAR&I ) $%left( &&CHARLEN&I );
		%let SQZ_CHAR_FMT = &SQZ_CHAR_FMT %qtrim( &&CHAR&I ) $%left( &&CHARLEN&I ).;
	%end;
 
%if &N_NUM > 0 %then
	%do I = 1 %to &N_NUM;
		%let SQZ_NUM = &SQZ_NUM %qtrim( &&NUM&I ) &&NUMLEN&I;
	%end;
 
data _null_;
	length retain $32767;
	retain retain 'retain ';
	dsid = open( "&DSNIN", 'I' );
 
	do _i_ = 1 to attrn( dsid, 'nvars' );
		retain = trim( retain ) || ' ' || varname( dsid, _i_ );
	end;
 
	call symput( 'RETAIN', retain );
run;
 
%if &syserr > 1 %then
	%do;
		%let etape=  6;
		%goto erreur;
	%end;
 
data &DSNOUT (compress=&COMPRESS);
	&RETAIN;
 
	%if &N_CHAR > 0 %then
		%str( &SQZ_CHAR ;     );
 
	%if &N_NUM  > 0 %then
		%str( &SQZ_NUM ;      );
 
	%if &N_CHAR > 0 %then
		%str( &SQZ_CHAR_FMT ; );
	set &DSNIN;
run;
 
%if &syserr > 4 %then
	%do;
		%let etape=  7;
		%goto erreur;
	%end;
%goto fin;
 
%erreur:
%put %str(traitement interrompu à l%'étape) &etape;
 
%fin:
options &_notes &_spool &_source &_symbolgen &_mprint &_mfile;
%mend OPTIMUM;


Le second programme plus léger - approche différente.

Usage :

Code :Sélectionner tout -Visualiser dans une fenêtre à part
1
2
3
4
%resize (
              data=sashelp.class, 
              out=work.class
             );


Le code (2) :
Code :Sélectionner tout -Visualiser dans une fenêtre à part
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
%macro resize (DATA=, out=);
 
  %IF (%superq(DATA)= OR %superQ(OUT)=) %then %do;
    %put You must specify BOTH DATA= AND out=;
    %goto ByeBye;
  %end;
 
  proc contents noprint DATA=&DATA out=_contnt_(INDEX=(varnum));
  run;
 
  %LOCAL dsid nvars rc needed;
 
  %let dsid=%sysfunc(open(_contnt_));
  %IF &dsid %then %do;
    %let nvars=%sysfunc(attrn(&dsid,NOBS));
    %let rc=%sysfunc(close(&dsid));
  %end;
  %else %goto ByeBye;
 
  %LOCAL zero;
  %do i=1 %TO &nvars;
    %LOCAL var&i typ&i len&i mln&i;
  %end;
 
  DATA _null_;
    SET _contnt_;
    call symput ('var'||LEFT(_n_), name);
    IF type=2
      then call symput ('typ'||LEFT(_n_), '$');
      else call symput ('typ'||LEFT(_n_), ' ');
    call symput ('len'||LEFT(_n_), length);
  run;
 
  proc sql noprint;
    DROP TABLE _contnt_;
 
    SELECT
    0
    %do i=1 %TO &nvars;
      %IF &&typ&i=$ %then ,max(length(&&var&i)) ;
    %end;
    INTO
    :zero
    %do i=1 %TO &nvars;
      %IF &&typ&i=$ %then ,:mln&i ;
    %end;
    FROM &DATA
    ;
  quit;
 
  %let needed = 0;
  %do i = 1 %TO &nvars;
    %IF &&typ&i=$ %then
      %let needed = %eval (&needed OR (&&mln&i < &&len&i));
  %end;
 
  %IF (%superq(DATA) ne %superq(out)) OR &needed %then %do;
 
    %IF NOT &needed %then
      %put NOTE: There IS nothing TO resize, but OUT IS different than DATA;
 
    DATA &OUT;
      retain %do i=1 %TO &nvars; &&var&i %end;;
      length %do i=1 %TO &nvars;
                %IF &&typ&i=$ %then &&var&i $ &&mln&i ;
             %end;;
      SET &DATA;
    run;
 
  %end;
  %else %do;
    %put NOTE: OUT IS same AS DATA (&DATA) AND DATA has no slack, resize NOT done.;
  %end;
 
%ByeBye:
 
%mend resize;