A LENGTH statement for a numeric variable that is positioned after a SET statement might cause truncation of data and no warning message is issued


Truncation of data can occur and no warning message is issued if a LENGTH statement for a numeric variable is positioned after a SET statement in a DATA step.

The following example code illustrates the problem:

data test1;
   length var1 $10 var2 8;
   do i = 1 to 50;
      var1='hello';
      var2=65537;
      output;
   end;
run;

data test2;
   set test1;
   length var2 3;
   by var2;
run;

To circumvent the problem, place the LENGTH statement before the SET statement in your DATA step code.

When a LENGTH statement is positioned before a SET statement, the following warning is generated if the length of the variable in the LENGTH statement is shorter than the specified variable being read in with the SET statement:

WARNING: Multiple lengths were specified for the BY variable var2 by input data sets and LENGTH, FORMAT, INFORMAT, or ATTRIB statements. This may cause unexpected results.

The following example code illustrates this:

data test1;
   length var1 $10 var2 8;
   do i = 1 to 50;
      var1='hello';
      var2=65537;
      output;
   end;
run;

data test2; 
   length var2 3;
   set test1 ;
   by var2;
run;