Previous Page | Next Page

Understanding DATA Step Processing

Defining Enough Storage Space for Variables

The first time that a value is assigned to a variable, SAS enables as many bytes of storage space for the variable as there are characters in the first value assigned to it. At times, you may need to specify the amount of storage space that a variable requires.

For example, as shown in the preceding example, the variable Remarks contains miscellaneous information about tours:

if Vendor = 'Hispania' then Remarks = 'Bonus for 10+ people';

In this assignment statement, SAS enables 20 bytes of storage space for Remarks as there are 20 characters in the first value assigned to it. The longest value may not be the first one assigned, so you specify a more appropriate length for the variable before the first value is assigned to it:

length Remarks $ 30;

This statement, called a LENGTH statement, applies to the entire data set. It defines the number of bytes of storage that is used for the variable Remarks in every observation. SAS uses the LENGTH statement during compilation, not when it is processing statements on individual observations. The following DATA step shows the use of the LENGTH statement:

options pagesize=60 linesize=80 pageno=1 nodate;
data newlength;
   set mylib.internationaltours;
   length Remarks $ 30;
   if Vendor = 'Hispania' then Remarks = 'Bonus for 10+ people';
   else if Vendor = 'Mundial' then Remarks = 'Bonus points';
        else if Vendor = 'Major' then Remarks = 'Discount for 30+ people';
run;

proc print data=newlength;
   var Country Vendor Remarks;
   title 'Information About Vendors';
run;

The following output displays the NEWLENGTH data set:

Using a LENGTH Statement

                           Information About Vendors                           1

             Obs    Country    Vendor      Remarks

              1     France     Major       Discount for 30+ people
              2     Spain      Hispania    Bonus for 10+ people   
              3     India      Royal                              
              4     Peru       Mundial     Bonus points           

Because the LENGTH statement affects variable storage, not the spacing of columns in printed output, the Remarks variable appears the same in Efficient: Using Variables to Contain Maximum Information and Using a LENGTH Statement. To show the effect of the LENGTH statement on variable storage using the DATASETS procedures, see Getting Information about Your SAS Data Sets.

Previous Page | Next Page | Top of Page