SAS OnlineTutor HomeFAQ PageSuggested Learning PathsContents+Searchback||next

Creating Variables
Specifying Lengths for Variables


You can use a LENGTH statement to specify a length (the number of bytes) for Type before the first value is referenced elsewhere in the DATA step.


General form, LENGTH statement:
LENGTH variable(s) length;

where

  • variable(s) names the variable(s) to be assigned a length
  • length is an integer that specifies the length of the variable.  
NOTE: To specify the length of a character variable, add a dollar sign  ($) after the variable name:   LENGTH variable $ length;


Examples:
     length Type $ 8;
     length Address1 Address2 Address3 $ 200;
     length FirstName $ 12 LastName $ 16;

Within your program, you include a LENGTH statement to assign a length to accommodate the longest value of the variable Type. The longest value is Variable, which has eight characters. Because Type is a character variable, you must follow the variable name with a dollar sign ($).

     data finance.newloan;
        set finance.records(drop=amount rate);
        length Type $ 8;
        TotalLoan+payment;
        if code='1' then Type='Fixed';
        else Type='Variable';
     run;
NOTE: Make sure the LENGTH statement appears before any other reference to the variable in the DATA step. The length of a new variable is determined by the first reference in the DATA step. If the variable has been created by an assignment statement, later use of the LENGTH statement will not alter its size.

Now that you have added the LENGTH statement to your program, the values of Type are no longer truncated.

SAS Data Set Finance.Newloan
Account Months Payment Code TotalLoan Type
101-1092 60 467.43 1 467.43 Fixed
101-1731 360 958.57 2 1426.00 Variable
101-1289 36 325.02 2 1751.02 Variable
101-3144 12 308.52 1 2059.54 Fixed
103-1135 24 403.47 1 2463.02 Fixed
103-1994 60 393.07 2 2856.10 Variable
103-2335 48 128.02 1 2984.11 Fixed
103-3864 360 735.75 1 3719.86 Fixed
103-3891 360 257.75 2 3977.61 Variable



back||next


Copyright © 2002 SAS Institute Inc., Cary, NC, USA. All rights reserved.

Terms of Use & Legal Information | Privacy Statement