Specifying Lengths for Variables

Avoiding Truncated Variable Values

Although it is possible to add IF-THEN and ELSE statements to a DATA step in order to create the variable TestLength, consider what happens when you submit the following program:
data clinic.stress; 
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43
         Tolerance $ 45; 
   TotalTime=(timemin*60)+timesec; 
   retain SumSec 5400; 
   sumsec+totaltime; 
   if totaltime>800 then TestLength='Long'; 
   else if 750<=totaltime<=800 then TestLength='Normal'; 
   else if totaltime<750 then TestLength='Short'; 
run;
During compilation, when creating a new character variable in an assignment statement, SAS allocates as many bytes of storage space as there are characters in the first value that it encounters for that variable. In this case, the first value for TestLength occurs in the IF-THEN statement, which specifies a four-character value (Long). So, TestLength is assigned a length of 4, and any longer values (Normal and Short) are truncated.
Figure 11.2 Truncated Variable Values (partial output)
Partial Output: Truncated Variable Values
The example above assigns a character constant as the value of the new variable. You can also see other examples of the default type and length that SAS assigns when the type and length of a variable are not explicitly set.
Tip
You can use a LENGTH statement to specify a length (the number of bytes) for TestLength before the first value is referenced elsewhere in the DATA step.
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.

Examples: LENGTH Statement

Here is a variable list in which all three variables are assigned a length of $200.
length Type $ 8;
length Address1 Address2 Address3 $200;
length FirstName  $12 LastName $16;
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 clinic.stress; 
   infile tests; 
   input ID $ 1-4 Name $ 6-25 RestHR 27-29 MaxHR 31-33
         RecHR 35-37 TimeMin 39-40 TimeSec 42-43 
         Tolerance $ 45; 
   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 11.3 Variable Values That Are Not Truncated (partial output)
Partial Output: Variable Values that are Not Truncated
Last updated: January 10, 2018
..................Content has been hidden....................

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