Discussion:
Is there a way to remove trailing zeros from a numeric
(too old to reply)
Nordlund, Dan (DSHS/RDA)
2009-03-31 16:44:13 UTC
Permalink
-----Original Message-----
Brian Wallace
Sent: Tuesday, March 31, 2009 9:28 AM
Subject: Is there a way to remove trailing zeros from a numeric variable?
I keep running up against the defined format. I've tried converting the numeric
variable back to a character using the BEST format then BACK to a numeric but I
keep losing precision.
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
49.9568
50.3701
6.43636
4.48661
0.0297726
but still remain numbers.
Any suggestions would be greatly appreciated.
Thank you,
Brian Wallace
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
Joe Matise
2009-03-31 16:36:21 UTC
Permalink
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

On Tue, Mar 31, 2009 at 11:27 AM, Brian Wallace
I keep running up against the defined format. I've tried converting the
numeric variable back to a character using the BEST format then BACK to a
numeric but I keep losing precision.
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
49.9568
50.3701
6.43636
4.48661
0.0297726
but still remain numbers.
Any suggestions would be greatly appreciated.
Thank you,
Brian Wallace
Nordlund, Dan (DSHS/RDA)
2009-03-31 18:50:34 UTC
Permalink
-----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
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
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
Mike Rhoads
2009-03-31 19:56:56 UTC
Permalink
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
j***@gmail.com
2009-05-19 18:50:12 UTC
Permalink
Post by Mike Rhoads
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/sasdecalignhttp://groups.google.com/group/comp.soft-sys.sas/search?group=comp.so...
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
-----Original Message-----
Sent: Tuesday, March 31, 2009 2:51 PM
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
Hi,

I'm pretty sure that the ROUND function will perform the action that
you require, for example:

The numeric variable LOGCONC is being converted to the character
variable LOGCC

logcc = put(left(trim(round(logconc, .001))), 5.3) ;

Best,
John

Schwarz, Barry A
2009-03-31 23:11:03 UTC
Permalink
Attack the problem in two parts.

First decide how you want things to line up with the extra zeros
printed. For your sample data, it looks the format 10.7 would do this.

Then use this format to produce a character variable of this length
c = put(n, 10.7);

Then a sequence like the following should do what you want
Set index to last character
While character at index is 0
Change character to blank
Decrement index

Be aware that very few floating point numbers can be represented
exactly. It is entirely possible that an input of 12.1 when converted
to numeric and then converted back by put could result in a value like
12.1000023 depending on the number of significant digits your system
provides. At this point, there is no way for you to distinguish between
an input of 12.1 and an input of 12.1000023.

-----Original Message-----
From: Brian Wallace
Sent: Tuesday, March 31, 2009 9:28 AM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Is there a way to remove trailing zeros from a numeric
variable?

I keep running up against the defined format. I've tried converting the
numeric variable back to a character using the BEST format then BACK to
a numeric but I keep losing precision.

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.
Joe Whitehurst
2009-04-01 05:24:49 UTC
Permalink
I think you have discovered how the rebugnicans kept the deficit so low.
They just dropped a few trailing zeros!

On Tue, Mar 31, 2009 at 12:27 PM, Brian Wallace
I keep running up against the defined format. I've tried converting the
numeric variable back to a character using the BEST format then BACK to a
numeric but I keep losing precision.
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
49.9568
50.3701
6.43636
4.48661
0.0297726
but still remain numbers.
Any suggestions would be greatly appreciated.
Thank you,
Brian Wallace
Continue reading on narkive:
Search results for 'Is there a way to remove trailing zeros from a numeric' (Questions and Answers)
12
replies
How much is $0.010 worth?
started 2010-07-05 17:12:38 UTC
advertising & marketing
Loading...