Date: Tue, 31 Mar 2009 11:50:34 -0700
Reply-To: "Nordlund, Dan (DSHS/RDA)" <NordlDJ@DSHS.WA.GOV>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Nordlund, Dan (DSHS/RDA)" <NordlDJ@DSHS.WA.GOV>
Subject: Re: Is there a way to remove trailing zeros from a numeric
variable?
In-Reply-To: <191957.85954.qm@web50907.mail.re2.yahoo.com>
Content-Type: text/plain; charset=windows-1252
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of
> Brian Wallace
> Sent: Tuesday, March 31, 2009 11:36 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Is there a way to remove trailing zeros from a numeric variable?
>
> Thank you for all of your help and responses.
>
> > An example. I've converted these character variables
> > to numeric variables:
> >
> > character variables --> converted to -->
> > numeric variables
> >
> > 49.9568 sq. cm 49.9568000
> > 50.3701 sq. cm 50.3701000
> > 6.43636 sq. cm 6.4363600
> > 4.48661 sq. cm 4.4866100
> > 0.0297726 sq. cm 0.0297726
> >
> > Now, I need to remove those trailing zeros, yet keep the
> > variable a numeric variable and not loose any precision. I
> > need them to look like:
> >
> > 49.9568
> > 50.3701
> > 6.43636
> > 4.48661
> > 0.0297726
> >
> > but still remain numbers.
> >
>
>
>
> > It rather depends on what you're planning on doing with
> > the number. In
> > what, precisely, do you want to see the number formatted
> > that way? In a
> > proc print/freq/means/report/etc.? In an export to another
> > data type
> > (excel, access, spss, sql, etc.)?
> >
> > -Joe
> >
>
> Joe,
>
> The communication is still a bit hazy but aparently the client wants the variable to
> be converted to a numeric value but look EXACTLY the same. It's read in as a
> character field, then just output as a SAS data set.
>
> > Brian,
> >
> > How did you convert the character values to numeric? Are
> > you sure you were successful? When displaying numeric
> > values without specifying a format, SAS won't display
> > trailing zeros. Are you using a format that specifies more
> > decimal places than you have? Can you show us a
> > reproducible example?
> >
> > For example, in the following code I don't get trailing
> > zeros.
> >
> > Data test;
> > Char = '49.9568';
> > Num = input(char,best.);
> > Put num= ;
> > Run;
> >
> > Dan
> >
> > Daniel J. Nordlund
> > Washington State Department of Social and Health Services
> > Planning, Performance, and Accountability
> > Research and Data Analysis Division
> > Olympia, WA 98504-5204
> >
>
> Daniel,
>
> Yes, I did specify a format. Which is why the question I'm asking seems so stupid.
> If you define a format, SAS will stick the number into that format according to the
> format's specifications. I defined the format as 12.7 (to pick up the complete value
> in the last record of my example, 0.0297726) So, naturally, SAS gave every piece
> of data that format when using the INPUT function. It turned the character string
> "50.3701" into 50.3701000 (total 12 places, 7 after the decimal.) I just thought
> maybe there was a numeric "TRIM" function or something to get rid of those
> excess zeros.
>
> I guess one solution is to check the number of values after the decimal point and
> then write a CASE statement or something with the input function. Example:
>
> I would send "50.3701" (four digits after the decimal) to INPUT(var,9.4) while
> sending "0.0297726" (seven digits after the decimel) to INPUT(var,12.7), etc.
>
> Thanks again for your help,
>
> Brian Wallace
Brian,
Rather than using an informat of 12.7, you might try a best. Informat.
data want;
length char $12;
input char $;
num = input(char,best.);
cards;
49.9568
50.3701
6.43636
4.48661
0.0297726
;
run;
proc print;
format num best.;
run;
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204
|