Date: Thu, 11 Mar 2004 11:43:55 -0500
Reply-To: "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Richard A. DeVenezia" <radevenz@IX.NETCOM.COM>
Subject: Re: Substring Puzzle
Chang Y. Chung wrote:
> On Wed, 10 Mar 2004 18:01:29 -0500, Richard A. DeVenezia
> <radevenz@IX.NETCOM.COM> wrote:
>
>>> A compiler change yes. But is it a bug fix or a clarified feature.
>>> Why
> is
>> z still 12 length?
>
> Hi,
>
> I have a hypothesis. if c is not initialized but used in an
> expression, then sas assumes a numeric variable and convert it to a
> string using best12 format. This is kind of consistent with the
> following results, too. This is not a definitive test of this
> hypothesis, but...
>
> Cheers,
> Chang
>
> data _null_;
>
> z = substr(c,1,1);
> zz = trimn(d);
>
> length_z = vlength(z);
> length_zz = vlength(zz);
>
> put (_all_) (= +(-1) "***" /);
>
> run;
> /* on log
> z=
> c=.
> zz=.
> d=.
> length_z=12
> length_zz=12
> */
Chang:
Superb. I definitely misanalyzed it.
> z=substr(c,1,1);
> length_c = vlength(c);
> length_z = vlength(z);
> put (length:)(=/);
> c=8, first usage context is character parameter (causing a default length
8)
*wrong
> z=12, huh ? first usage context is character as return from var. At this
*huh, wrong
c=8 because it's first use context is numeric with implicit best12.
conversion when used in a character role
z=12 because implicit numeric to character conversions are via best12.
format
I believe it is correct to say any first use on the right side of equals
always causes a variable to be attributed as a numeric type. First use
context on the left side is based on type of right side.
Use default=4 (thanks Ron/Dianne, something new learned) to make numerics
even more obvious.
data _null_;
length default=4;
substr(a,1,1) = substr(b,1,1);
c = substr(d,1,1);
alen = vlength(a);
blen = vlength(b);
clen = vlength(c);
dlen = vlength(d);
atyp = vtype(a);
btyp = vtype(b);
ctyp = vtype(c);
dtyp = vtype(d);
put (a:)(=) / (b:)(=) / (c:)(=) / (d:)(=);
run;
----
a= alen=8 atyp=C
b=. blen=4 btyp=N
c= clen=12 ctyp=C
d=. dlen=4 dtyp=N
--
Richard A. DeVenezia
http://www.devenezia.com/