Date: Sat, 24 Sep 2011 15:04:40 -0400
Reply-To: Arthur Tabachneck <art297@ROGERS.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Arthur Tabachneck <art297@ROGERS.COM>
Subject: Re: When using length statement doesn't change format width
Thanks for the response, and I don't disagree with your recommended
approach, but the behavior is not that definitive. In fact, it highlights a
rather major (in my opinion) error in the system.
If you run the following code and, after each of the datasteps, bring up the
file in SAS Explorer, I think that you will see what I mean.
length name $30;
proc contents data=test details fmtlen out=out1;
format name $30.;
proc contents data=test details fmtlen out=out2;
If you open file test after the first datastep you will see that name is
shown to have a length of 30 and a format of $30. Out1, on the other hand,
shows that ONLY the length was set and that no format was assigned.
If you open file test after the second datastep you will see that name again
is shown to have a length of 30 and a format of $30. Out2, similarly, shows
that BOTH the length was set and that a $30. format was assigned.
As a non-programmer I have always relied on the method of bringing up a file
with SAS Explorer, right clicking on a particular variable, and selecting
column attributes. If one does that after having run the first datastep,
they will see that length is shown as 30, and both the format and informat
are shown as being $30.
Either proc contents is showing the wrong information, or the SAS Explorer
is showing the wrong information. I don't know which is correct but, if one
also changes one of the names to being a longer name, I'm led to think that
proc contents is the one showing the incorrect values. E.g., if you run:
length name $30;
if _n_ eq 3 then name=
"FirstName MI VeryLongLastName";
proc contents data=test details fmtlen out=out3;
the same contradictory results are seen from the two methods (i.e., SAS
Explorer and proc contents), but the full name is shown in SAS Explorer and,
if you then run a proc print, the full name is also displayed.
On Sat, 24 Sep 2011 16:50:58 +0000, DUELL, BOB <bd9439@ATT.COM> wrote:
>I don't know of any documentation that describes this behavior exactly as
you describe, but it is expected. There are four different attributes
associated with each SAS variable (LENGTH, FORMAT, INFORMAT, and LABEL) and
each has a corresponding SAS statement that can be used to change the
attribute. In other words, using the LENGTH statement only changes the
length, it does not affect the other attributes.
>For this reason, I almost always use the ATTRIB statement to define
"production" SAS variables in each data set. The ATTRIB statement allows
one to define all the attributes at once. For my very formal jobs (for each
permanent SAS dataset going to a "production" library), I follow up the
section of ATTRIB statements with a KEEP statement; although it does require
a bunch of typing, it serves as very good in-code documentation. And the
KEEP statement lets me use any other "temporary" variables in the code
without them showing up in my production data.
>I don't believe that using a LENGTH statement has ever simultaneously
changed the variable format. Note that the FORMAT attribute is optional.
My guess is that when you changed a variable length in the past and thought
the format length had also changed, it was because the variable did not have
any format specification to begin with. I could be wrong historically
(perhaps it did so in a previous SAS release).
>Hope this helps,
>From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Arthur
>Sent: Saturday, September 24, 2011 7:16 AM
>Subject: When using length statement doesn't change format width
>Can someone point me to documentation that talks about when using a length
>statement, before a set statement in a data step, doesn't change the width
>the format statement?
>My experience has always been that using a length statement to change the
>width of a character variable results in simultaneously changing the width
>the format. However, I came across a case yesterday where that didn't
>Using a format statement, though, ended up changing both.
>Is that particular behavior documented somewhere?
>Thanks in advance,