The 'length' Statement


Consider the following data step:

 

data region;

input urban_area $;

cards;

Atlanta

Boston

Los_Angeles

Seattle

Washington_DC

;

run;

 

 

What looks wrong here?

 

In printing these data, the value of the variable urban_data has been cut off. To prevent this, you must use a length statement when creating character variables.

 

data region;

length urban_area $13;

input urban_area $;

cards;

Atlanta

Boston

Los_Angeles

Seattle

Washington_DC

;

run;

The length statement specifies the maximum length for the values of a variable. The length statement should come at the beginning of the data step, before the variables for which the lengths are being set are defined.

This is true for variables entered using an input statement, or those created in a data step.

Recall the example in which we categorized grades into group;

 

data grades;

input name $ gpa;

if gpa<3.0 then group = "not good standing";

if gpa>=3.0 then group = "good standing";

cards;

Ann 3.7

Bart 2.9

Cecil 3.5

Denise 4.0

Emily 2.5

Frank 3.6

;

run;

proc print;

run;

 

Why were the character variable values not truncated?

SAS will use the first value it encounters if there is no length statement. So, in the example, the first value is "not good standing," and so the length is set at 17, which is more than enough for the value "good standing".

If we had instead reversed the two lines of code, the length of the variable group would be set to 13, and some of the values would be truncated.

 

data grades;

input name $ gpa;

if gpa>=3.0 then group = "good standing";

if gpa<3.0 then group = "not good standing";

cards;

Ann 3.7

Bart 2.9

Cecil 3.5

Denise 4.0

Emily 2.5

Frank 3.6

;

run;

If you are accessing an already created SAS data set (temporary or permanent), you do not have to use a length statement, as the length is stored with the SAS data set.

 

Note: Output can build up in the Results Viewer and you cannot clear it as you can the output window. To clear the Results Viewer, use the following two ODS statements. The first closes the viewer and the second opens it, clearing it as it opens.

 

ods html close;

ods html;

run;