Changing a variable length without getting a warning

5.8k views Asked by At

I have a variable called ID on dataset OLD, and I need to change the length from 13 to 12 for a merge. Below is the metadata associated with this variable.

Variable Type Len Format Informat Label 
ID       Char  13   $12.     $12. 'Person ID'

(The contents of each value for ID is always exactly 12 characters in this file)

When I try to edit the length before the set statement in a DATA step, I get a warning. The same happens when I edit both the length and the format.

data NEW;
length ID $12;
format ID $12.;
set OLD;
run;

...
WARNING: Multiple lengths were specified for the variable AN_RESEARCHID by 
             input data set(s). This can cause truncation of data.

Examples from SAS community forums don't seem to describe why this warning would occur, or how to avoid it. Any thoughts?

3

There are 3 answers

1
Amw 5G On BEST ANSWER

You could rename the original variable in your SET statement, and create a new variable with your desired length. Then set the new variable equal to the old within the data step. You won't get the warning. But of course what it warns you about still holds- that is, if your data does happen to have 13 characters, it'll truncate the value when jamming it into a variable with a shorter length.

data NEW(drop=ID_orig);
length ID $12;
set old(rename=(ID=ID_orig));
ID= ID_orig;
run;
1
Jay Corbett On

The answer given by Amw5G is how I would solve this problem if I could not correct the original length setting of ID. However, you should investigate changing the original length assignment in the prior step.

Some reasons that a variable receives an unwanted length are:

If a character VAR is assigned to a value with no LENGTH statement then the VAR will take the length of the first value assigned to it. EXAMPLE

If a character VAR is assigned by a function, like SCAN, it can receive a length of 200. The CAT function will return a length of the sum of the lengths of the values which are being concatenated. EXPLANATION

0
Dominic Comtois On

There is a SAS option called varlenchk. By setting it temporarily to NOWARN, you can change lengths without any warning.

options varlenchk = nowarn; * Turn off the warning prior to your operation;

/* your operations here */

options varlenchk = warn; * put it back on to be safe!;

See this link for details:

http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003269301.htm