Date: Fri, 16 Aug 1996 08:25:17 EDT
Reply-To: rhoadsm1@WESTATPO.WESTAT.COM
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Mike Rhoads <rhoadsm1@WESTATPO.WESTAT.COM>
Subject: Re[2]: CORRECTION: reading dollar amounts with SAS
[OK -- small correction to my original posting, in case anyone winds up
searching in vain for the mythical "DOLLAR" informat. What I REALLY meant
was the COMMA informat, which among its many virtues removes dollar signs
from input data fields. I guess there's a reason to try things out before
posting them ...]
Mike Rhoads
It was interesting to read Pete Lund's solution to Bill Kossack's question
about reading values such as 0000-$10.09 and 00$12345.43 in SAS. (Pete's
solution and Bill's original question are included at the end.) I had been
thinking of a different approach: after reading the field as character, use a
DO-loop to replace all of the leading zeroes with blanks, and then use the INPUT
function with the COMMA informat. I appreciate Pete's ingenious alternative,
and since it's at least as good as mine I felt relieved of the obligation to
actually implement my idea in code.
One final thought: we really should find the "someone" who created these
atrocities, and stop him (or her) before s/he strikes again!!! (wink)
Mike Rhoads
Westat
RhoadsM1@Westat.com
*** Pete Lund's response and Bill Kossack's original question ***
Bill-
Try this.
data _null_;
input dollars $12.;
NewBucks = input(compress(dollars,'$-'),12.2) *
(((indexc(dollars,'-') gt 0) * -2) + 1);
put dollars= NewBucks=;
cards;
0000-$10.09
00$12345.43
;
run;
with results...
DOLLARS=0000-$10.09 NEWBUCKS=-10.09
DOLLARS=00$12345.43 NEWBUCKS=12345.43
Read in the dollar values as character. The statement to convert the
dollars to numeric does the following:
- COMPRESSes out the $ and the - sign and puts the number into a numeric
format - this will get rid of the leading zeros
- checks to see if there's a - sign (INDEXC will return a zero if not or
the position of the - in the string), here's the logic:
-> INDEXC(dollars,'-') gt 0 returns 0 if no '-', 1 if found
-> multiply by -2 still gives 0 if not found, -2 if found
-> add 1 gives 1 if not found, -1 if found
- we can multiply the numeric portion of the dollar amount by this number
to get a negative if there was a '-' in the number and a positive if not
You could also do the first part of the expression:
NewBucks = input(compress(dollars,'$_'),12.2);
and then:
if indexc(dollars,'-') gt 0 then NewBucks = NewBucks * -1;
But where's the fun in that?!?
Pete Lund
WA State Office of Financial Management
peterl@ofm.wa.gov
----------
From: Bill Kossack
Subject: reading dollar amounts with SAS
Date: Thursday, August 15, 1996 2:39PM
Someone has created a file containing a field with an amount of money.
The problem with reading the field is that they have written the field in
the following format.
0000-$10.09
or
00$12345.43
That is, the dollar amount has leading zeros with a sign if negative
and a dollar sign and a decimal and the cents.
The number of dollars can range from none to a large number. This results
in the sign being in any possible location and the dollar sign following.
The kicker is the leading zeros.
How do I read this with SAS?
William S. Kossack
Lakewood, Colorado
kossack@netcom.com