When I had a quick glance at the original question, I said to myself: This must be easy -- surely there's a format for that.
Well, it isn't, and there's not. :-( Note that neither BESTD. nor D. produce the deisred result.
I think the INPUT is a red herring. If I understand the question correctly, it's not the internal numeric representation of the value that's the issue (which will be the same regardless), or even whether or not it started out as character. Rather, it is how to get a column of numbers to display with aligned decimal points, while suppressing unnecessary trailing zeroes.
I can understand the lack of a format -- the alignment is a tricky issue, and implementing it probably differs by the type of output desired (listing, HTML, RTF, PDF, ...). Is this "left" aligned or "right" aligned -- really neither. The only general format that I can think that SAS might come up with would depend on sending digit-width spaces (actually a Unicode character) to the output, and that seems a little esoteric.
This has been discussed before: http://tinyurl.com/sasdecalign
http://groups.google.com/group/comp.soft-sys.sas/search?group=comp.soft-sys.sas&q=decimal+alignment&qt_g=Search+this+group
There is an RTF-specific solution (I did get this to work for me once upon a time). Otherwise, keeping a suitably-aligned "Parallel" character variable, as has been suggested, seems to be the only solution.
Mike Rhoads
***@Westat.com
-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of Nordlund, Dan (DSHS/RDA)
Sent: Tuesday, March 31, 2009 2:51 PM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Re: Is there a way to remove trailing zeros from a numeric variable?
-----Original Message-----
Brian Wallace
Sent: Tuesday, March 31, 2009 11:36 AM
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
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
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
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