On Tue, 29 Nov 2005 13:40:45 -0800, james.p.campbell@GMAIL.COM wrote:
>I have a file with fixed-width data, and the variables are padded with
>spaces, for example
>Value1 Value2 Value3
>Val_a Val_b Val_c
>I read in the file using an infile statement:
>infile line $27.;
>and I want to extract the contents of the first variable. However,
>using substr(line,1,9), gives me only "Value1" and "Val_a" without the
>trailing spaces. How can I preserve them?
>As a note, simply breaking the data apart at the infile, e.g.
>infile var1 $9. var2 $9. var3 $9.;
>seems to have the same problem.
If you do below, then you are creating a character type variable, var1, of
the length of 27 characters, since the variable, line, is of 27-character long.
var1 = substr(line, 1, 9);
So, the way I look at it, you not only have the trailing blanks, but you
have many more than you seem to expect. :-)
The second method:
infile var1 $9.;
will indeed create a variable with a length of 9 characters. The $9
informat, however, will trim the leading blanks. But it will keep the
Well, I should have said, first, that in SAS, once the character values are
assigned to a variable, then there is no difference between the trailing
blank characters and nothing -- since SAS's character variable is of a fixed
length and is always padded right by blank characters. For example, if a
variable is of length $5, then any of the following assignments will result
in the same character values stored, namely "abc" followed by two space
length var1 $5;
var1 = "abc";
var1 = "abc ";
var1 = "abc ";
I am not sure if this makes sense to you, but hope it helps at least a bit...