Select Your Region
Americas
Europe
Middle East & Africa
Asia Pacific
![]() | ![]() | ![]() | ![]() |
This sample code below finds the maximum length of each character variable in the data set, writes a DATA step that issues new LENGTH and FORMAT statements for each character variable, and then reads the data. The goal is to make the data set as small as possible without using compression.
The first DATA step counts the number of character variables in the data set and stores this value in a macro variable. The next step loops through all of the data looking for the maximum number of characters in each variable. When all of the rows have been checked, a LENGTH and FORMAT statements are generated for each variable and saved in macro variables. In the final DATA step, the macro loop pushes the LENGTH and FORMAT statements out to the DATA step ahead of the SET statement so that the length and format are changed.
To avoid the following WARNING, add this OPTIONS statement to the top of your program: OPTIONS VARLENCHK=NOWARN;
Note that a new data set is created rather than overwriting the original. If you prefer to replace the original, remove the underscore from the final DATA statement.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.