Specifying Lengths for Variables

Avoiding Truncated Variable Values

During the compilation phase, use an assignment statement to create a new character variable. SAS allocates as many bytes of storage space as there are characters in the first value that it encounters for that variable.
In the following figure, the variable TestLength has a length of four bytes. The word Short is truncated because the word Norm uses four bytes.
Figure 9.1 Truncated Variable Values (partial output)
Partial Output: Truncated Variable Values
When you assign a character constant as the value of the new variable, use the LENGTH statement to specify a length to avoid truncation of your values.
Syntax, LENGTH statement:
LENGTH variable(s) <$> length;
  • variable(s) names the variable or variables to be assigned a length.
  • $ is specified if the variable is a character variable.
  • length is an integer that specifies the length of the variable.
Here is a variable list in which all three variables are assigned a length of $200.
length Address1 Address2 Address3 $200;

Example: LENGTH Statement

Within the program, a LENGTH statement is included to assign a length to accommodate the longest value of the variable TestLength. The longest value is Normal, which has six characters. Because TestLength is a character variable, you must follow the variable name with a dollar sign ($).
Make sure the LENGTH statement appears before any other reference to the variable in the DATA step.
data stress; 
  set cert.stress; 
  TotalTime=(timemin*60)+timesec; 
  retain SumSec 5400; 
  sumsec+totaltime; 
  length TestLength $ 6; 
  if totaltime>800 then testlength='Long'; 
  else if 750<=totaltime<=800 then testlength='Normal'; 
  else if totaltime<750 then TestLength='Short'; 
run;
Note: If the variable has been created by another statement, then a later use of the LENGTH statement does not change its length.
Now that the LENGTH statement has been added to the program, the values of TestLength are no longer truncated.
Figure 9.2 Variable Values That Are Not Truncated (partial output)
Partial Output: Variable Values that are Not Truncated
Last updated: August 23, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.67.70