Reduce the length of all character variables

Character variable size is often large enough to facilitate the largest possible number of characters. Oracle, Microsoft Access, and Excel are the worst culprits. They even contain blank variables to be activated at a later date.

Changing the size of each character variable to the longest number of actual characters is a useful technique. It can significantly reduce the size of the file and assists when visually reviewing the data.

This macro has an optional to include the size of each variable and its new size in the SAS log.

SAS Fifth Dimension Macro-VAR_MAX_LEN_CHAR

%MACRO VAR_MAX_LEN_CHAR(INLOG=YES) ;

Parameter Description
INLOG Yes(the default) will include the size of each variable and its new size in the SAS log. INLOG=anything else will exclude it from the log.

Example

%MACRO VAR_MAX_LEN_CHAR(INLOG=YES) ; OPTIONS NOMPRINT ; %* ; %* Reduces the length of all character variables in the lastest SAS file ; %* ; RUN ; %LET LASTONE=&SYSLAST ; PROC CONTENTS NOPRINT OUT=CONTENTS ; PROC SORT DATA=CONTENTS ; BY LENGTH ; WHERE TYPE=2 ; DATA CONTENTS ; SET CONTENTS ; KEEP VNAME MEMNAME NAME TYPE LENGTH ; VNAME='NAME ' ; IF _n_<10 THEN SUBSTR(VNAME,LENGTH(VNAME)+1,1)=PUT(_n_,1.) ; ELSE IF _n_<100 THEN SUBSTR(VNAME,LENGTH(VNAME)+1,2)=PUT(_n_,2.) ; ELSE IF _n_<1000 THEN SUBSTR(VNAME,LENGTH(VNAME)+1,3)=PUT(_n_,3.) ; CALL SYMPUT(VNAME,COMPBL(NAME)) ; VNAME='FORMAT ' ; IF _n_<10 THEN SUBSTR(VNAME,LENGTH(VNAME)+1,1)=PUT(_n_,1.) ; ELSE IF _n_<100 THEN SUBSTR(VNAME,LENGTH(VNAME)+1,2)=PUT(_n_,2.) ; ELSE IF _n_<1000 THEN SUBSTR(VNAME,LENGTH(VNAME)+1,3)=PUT(_n_,3.) ; FORMAT FORMATS $12. ; FORMATS=FORMAT ; SUBSTR(FORMATS,LENGTH(FORMATS)+1)=COMPRESS(LENGTH) ; CALL SYMPUT(VNAME,FORMATS) ; CALL SYMPUT('NUM',_N_) ; RUN ; DATA _NULL_ ; SET &LASTONE END=EOF ; %IF &INLOG=YES %THEN %PUT %STR(----------->NUM=&NUM) ; %DO GII=1 %TO &NUM ; %*PUT %STR(----------->NAME&GII=&&NAME&GII) ; RETAIN MAX&GII ; DROP MAX&GII ; IF MAX&GIIMax length=&&MAX&GII OLD=&&FORMAT&GII of variable &&VAR_NAME&GII) ; %END ; DATA &LASTONE ; %DO GII=1 %TO &NUM ; FORMAT &&NAME&GII $&&MAX&GII... ; %END ; SET &LASTONE ; RUN ; OPTIONS MPRINT ; %MEND VAR_MAX_LEN_CHAR ;