LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (November 2005, week 5)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Tue, 29 Nov 2005 17:11:53 -0500
Reply-To:   Chang Chung <chang_y_chung@HOTMAIL.COM>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Chang Chung <chang_y_chung@HOTMAIL.COM>
Subject:   Re: Space omitted from substr()
Comments:   To: james.p.campbell@GMAIL.COM

On Tue, 29 Nov 2005 13:40:45 -0800, james.p.campbell@GMAIL.COM wrote:

>Hello -- > >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.

Hi, James,

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 "trailing" blanks.

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 characters.

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...

Cheers, Chang


Back to: Top of message | Previous page | Main SAS-L page