Newsgroups: bit.listserv.sas-l
Date:         Thu, 10 Sep 1992 16:54:19 EDT
Reply-To:     Philip Tejera <FXTBB@CUNYVM.BITNET>
From:         Philip Tejera <FXTBB@CUNYVM.BITNET>
Subject:      Previous postings about length

Thanks to Ben Conner for pointing out that the archives at Marist go back
further (to 86) than do the ones I checked. So here is Christian's answer
from 1988. Interesting how this has come up again after 4 years.

>>> Item number 1910, dated 88/07/25 11:48:42 -- ALL
Date:         Mon, 25 Jul 88 11:48:42 SET
Reply-To:     "Christian J. Reichetzeder" <REICHETZ@AWIIMC11>
From:         "Christian J. Reichetzeder" <REICHETZ@AWIIMC11>
Subject:      Re: A little Depth about Length

>Before we get too bored with repeating what the manuals say about Length and
>the precision of variables, let's think a minute :-)  ! As far as I know,
>IBM mainframes do floating point math on single (4-byte), double (8-byte),
>or extended (16-byte) precision variables. SAS doesn't seem to use the
>extended precision, so I'll stick to single and double precision. Fine.
>But wait..what about those other possible lengths 2,3,5,6,7 that we are only
>supposed to use for integers up to various sizes? HOW CAN THEY BE STORED AS
>FLOATING POINT? Or are they? Actually, the manual only talks about NUMERIC
>type variables. It says that SAS uses the (IBM) floating point instructions
>provided by the hardware (p. 228 Basics) for calculations, but I can't find
>anything about how it stores the values. Does it even store Length 4 or 8
>values as floating point, or does it use some other IBM or its own way of
>storing them? I don't think it truely uses IBM integer formats, because
>one can, for example, divide length 2 variables by 2 and get the right answer
>if the values are small enough. One can even divide such values by 3 and get
>the wrong answer ! (But close). Does anyone out there want to dive into this?

Sorry for repeating the whole text but I think it's needed.
Yes, IBM /370 has short(4), long(8)  and extended (16) floating point. I don't
know if  SAS internally  uses extended floating  point for  some calculations,
maybe.
To answer the question "how does SAS the LENGTH-trick" let's look:
a) at  the SAS  Basics (Vers 5),  page 157 (dealing  with LENGTH  and "Largest
   Integer Represented Exactly" (I take only 2-4, the table is up to 8).
           Length           LIRE
             2               255
             3            65,535
             4        16,777,215 .....
   Now if I'd use an INTEGER  (!) 2 bytes would hold -32768..+32767. Obviously
   SAS does NOT and NEVER use integer format.
b) at the representation of a /370 floating point(long form):
           +-+--------------+------/-------+
           |S|Characteristic|   Fraction   |
           +-+--------------|------/-------+
    Bits:  0 1              8             63
    "S" is the sign-bit. Characteristic  is the exponent in Excess-64 notation
    (meaning  exponent -64..+63  are mapped  to  0..127 resp.).  The point  is
    assumed immediately  to the  left of fraction.  Furthermore the  number is
    "normalized"  so that  the  leftmost  hex digit  is  non-zero to  maintain
    maximum precision  (i.e. at least one  of bits 8-11  is ^= 0 except  for a
    "true zero" - where also the Sign and Charac. are zero).
Therefore you end with
          number = (-1*(S=1))*Fraction*(16**(Characteristic-64))
Note that  the difference between short,  long and extended floating  point is
mainly precision,  not magnitude. When  storing a  number SAS simply  cuts the
string  to length  LENGTH  -  losing some  precision  but  nothing else.  When
fetching the numbers they're (I assume) simply padded on the right.
Now  we also  can see  the  correspondence between  LENGTH and  LIRE. You  can
exactly represent a (LENGTH-1)-Byte integer.
Deep enough?
Christian
P.S.: the biggest number with LENGTH 8  is approx. 7.237E+75, at LENGTH 2 it's
      7.209E+75, you  don't really lose  much :-).  And the smallest  value is
      something like 5.3976E-79 for all formats.
P.P.S.: oh yes,  it uses  "it's  own way"   for storing  missing values:  Zero
        Fraction and non-zero Sign/Characteristic.
Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU