How can I round an expression to 2 decimal points?

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

How can I round an expression to 2 decimal points?

Boika Mileva
Dear colleagues!

I have to calculate an expression into a variable with a FORMAT F6.2 and I
need it rounded to 2 decimal points. I expected to find a function
something like ROUND(VarName,2), but instead I found the functuon
 RND(numexpr), which rounds the numexpr to a whole number.

I would be grateful if some of you helps me to resolve my problem.

With best wishes!

Boika
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Spousta Jan
Hi Boika,

Compute rounded2 = RND(varname*100) / 100.

I agree with you that the optional second argument would be easier.

Greetings

Jan

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Boika Mileva
Sent: Friday, January 12, 2007 8:55 AM
To: [hidden email]
Subject: How can I round an expression to 2 decimal points?

Dear colleagues!

I have to calculate an expression into a variable with a FORMAT F6.2 and
I need it rounded to 2 decimal points. I expected to find a function
something like ROUND(VarName,2), but instead I found the functuon
RND(numexpr), which rounds the numexpr to a whole number.

I would be grateful if some of you helps me to resolve my problem.

With best wishes!

Boika
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Marta García-Granero
Hi Jan (and Boika)

Just for fun...

SJ> I agree with you that the optional second argument would be easier.

DEFINE !ROUND(!POS=!CHAREND('=')/!POS=!CHAREND(',')/!POS=!TOKENS(1)).
COMPUTE !1=RND(!2*(10**!3))/(10**!3).
LIST VAR=!2 !1 /CASES=FROM 1 TO 5.
!ENDDEFINE.

!ROUND rounded2=varname,2.

--
Best regards,
Marta
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Boika Mileva
In reply to this post by Boika Mileva
Thank you very much, Jan! It is so logic really, but it hasn't crossed my
mind!...

THANK YOU AND ALL THE BEST THROUGH THE WHOLE NEW 2007 YEAR!

Boika
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Spousta Jan
In reply to this post by Boika Mileva
A good one, Marta :-)

Have a nice weekend

Jan

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Marta García-Granero
Sent: Friday, January 12, 2007 9:43 AM
To: [hidden email]
Subject: Re: How can I round an expression to 2 decimal points?

Hi Jan (and Boika)

Just for fun...

SJ> I agree with you that the optional second argument would be easier.

DEFINE !ROUND(!POS=!CHAREND('=')/!POS=!CHAREND(',')/!POS=!TOKENS(1)).
COMPUTE !1=RND(!2*(10**!3))/(10**!3).
LIST VAR=!2 !1 /CASES=FROM 1 TO 5.
!ENDDEFINE.

!ROUND rounded2=varname,2.

--
Best regards,
Marta
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Boika Mileva
In reply to this post by Boika Mileva
Thank you, Marta!

It is really very fun and clever!

Best regards!

Boika
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Spousta Jan
In reply to this post by Boika Mileva
Just for the record, this can be even more powerful (and dangerous because it rewrites directly the variables in the list).

define rndmore (!pos=!char(',') / !pos = !cmd ).
* Rounds variables in the second argument to the precision given in the first argument.
* Both arguments separated by comma.
* The second argument can contain list of variables, and even use TO .
compute #@prec = 10**!1.
do repe @var = !2.
- compute @var = rnd(@var * #@prec) / #@prec .
end repe.
!enddefine.

GET FILE='C:\Program Files\SPSS\Cars.sav'.
rndmore -2, engine to weight year.
exe.

Greetings

Jan

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Marta García-Granero
Sent: Friday, January 12, 2007 9:43 AM
To: [hidden email]
Subject: Re: How can I round an expression to 2 decimal points?

Hi Jan (and Boika)

Just for fun...

SJ> I agree with you that the optional second argument would be easier.

DEFINE !ROUND(!POS=!CHAREND('=')/!POS=!CHAREND(',')/!POS=!TOKENS(1)).
COMPUTE !1=RND(!2*(10**!3))/(10**!3).
LIST VAR=!2 !1 /CASES=FROM 1 TO 5.
!ENDDEFINE.

!ROUND rounded2=varname,2.

--
Best regards,
Marta
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Richard Ristow
In reply to this post by Boika Mileva
At 02:54 AM 1/12/2007, Boika Mileva wrote:

>I have to calculate an expression into a variable with a FORMAT F6.2
>and I need it rounded to 2 decimal points.

At 03:14 AM 1/12/2007, Spousta Jan responded:

>Compute rounded2 = RND(varname*100) / 100.

which is, of course, how you do it - implemented particularly neatly by
Marta's elegant macro.

The other answer is, seriously consider whether you should do it at
all. If you use the F6.2 format, the value will display properly
rounded. Rounding the value isn't necessary; and it introduces a small
error in the value, if you're going to use it in later calculations.

I suggested this once to a poster who, it turned out, was implementing
an odd accounting algorithm, and needed to match its rounding rules
exactly. But if the rounding is just for display, it's generally better
not to.
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Boika Mileva
In reply to this post by Boika Mileva
Hi Richard!

Thank you very much for your additional notes! I agree with your remarks,
but actually I need my value really rounded, not only to be displayed
rounded. I am developing a data checking syntax and need to compare an
entered value with 2 decimal points in a particular variable, with the
value in another (control) variable, which is a result of a calculation,
using a formula. The second variable is with the same format as the first
(F6.2), but its real value (the not rounded result of the calculation)
could have more then 2 decimal points, so I will not have a full equality
in case of a correct entered value in the first variable. As a result - I
would illegally treat this value as an error.

Thank you once more time to all of you!

Boika
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Peck, Jon
Why not just make your test that the absolute value of the difference be <= .01 or so?

-Jon

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Boika Mileva
Sent: Sunday, January 14, 2007 7:33 AM
To: [hidden email]
Subject: Re: [SPSSX-L] How can I round an expression to 2 decimal points?

Hi Richard!

Thank you very much for your additional notes! I agree with your remarks,
but actually I need my value really rounded, not only to be displayed
rounded. I am developing a data checking syntax and need to compare an
entered value with 2 decimal points in a particular variable, with the
value in another (control) variable, which is a result of a calculation,
using a formula. The second variable is with the same format as the first
(F6.2), but its real value (the not rounded result of the calculation)
could have more then 2 decimal points, so I will not have a full equality
in case of a correct entered value in the first variable. As a result - I
would illegally treat this value as an error.

Thank you once more time to all of you!

Boika
Reply | Threaded
Open this post in threaded view
|

Suppress extra quotes in Export to CSV

King Douglas
I have an SPSS data file with one case of five long string variables.

If the long string variables are not enclosed in quotes and I SAVE TRANSLATE to a comma-delimited CSV file, the long string variables in the CSV file are simply separated by a comma, e.g.: long string 1,long string 2,long string 3, etc.

If the long string variables are enclosed in double quotes (e.g., "long string 1"), two additional sets of quotes are added in the CSV file, e.g.: """long string 1""","""long string 2""","""long string 3""", etc.

What's going on?  I want to append a single set of double quotes in the first example (because I have some commas in my long strings), or suppress the extra quotes in the second example.

Any ideas?

Thanks,

King Douglas
American Airline Customer Research
Reply | Threaded
Open this post in threaded view
|

Re: Suppress extra quotes in Export to CSV

Peck, Jon
From the SAVE TRANSLATE help.  QUALIFIER is on the text options subcommand.

QUALIFIER="char". Specifies the qualifier to use to enclose values that contain the delimiter character. The default is a double quote. The value must be a single character (single byte), enclosed in quotes (single or double quotes). Values that contain the qualifier character will also be enclosed by the qualifier and qualifiers within the value will be doubled.

Otherwise, the output would be ambiguous.  If your text contains double quotes, try using single quotes as the delimiter.

HTH,
Jon Peck

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of King Douglas
Sent: Sunday, January 14, 2007 11:49 AM
To: [hidden email]
Subject: [SPSSX-L] Suppress extra quotes in Export to CSV

I have an SPSS data file with one case of five long string variables.

If the long string variables are not enclosed in quotes and I SAVE TRANSLATE to a comma-delimited CSV file, the long string variables in the CSV file are simply separated by a comma, e.g.: long string 1,long string 2,long string 3, etc.

If the long string variables are enclosed in double quotes (e.g., "long string 1"), two additional sets of quotes are added in the CSV file, e.g.: """long string 1""","""long string 2""","""long string 3""", etc.

What's going on?  I want to append a single set of double quotes in the first example (because I have some commas in my long strings), or suppress the extra quotes in the second example.

Any ideas?

Thanks,

King Douglas
American Airline Customer Research
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Richard Ristow
In reply to this post by Boika Mileva
At 08:33 AM 1/14/2007, Boika Mileva wrote:

>Actually I need my value really rounded, not only to be displayed
>rounded. I am developing a data checking syntax and need to compare an
>entered value with 2 decimal points in a particular variable, with the
>value in another (control) variable, which is a result of a
>calculation, using a formula. The second variable is with the same
>format as the first (F6.2), but its real value (the not rounded result
>of the calculation) could have more then 2 decimal points.

That brings up another issue: SPSS numeric values are stored in binary
(the 64-bit floating-point representation from standard IEEE 754), and
a lot of two-decimal numbers, numbers that are an exact multiple of
10**-2, are not represented exactly. The rounding methods that have
been posted are the best possible; but the approximate two-decimal
numbers obtained by rounding won't necessarily be identical to the
approximate two-decimal numbers from a two-decimal value read in.

After

.  Compute rounded = RND(COMPUTED*100) / 100.

standard practice would use a test like

.  DO IF ABS(INPUT-ROUNDED) > 1E-4.

rather than

.  DO IF INPUT NE ROUNDED.

-Onward, and good luck,
  Richard
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Boika Mileva
In reply to this post by Boika Mileva
Thank you very much, Richard, for your precise addition! I will use your
advice!

With best wishes!

Boika
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Spousta Jan
In reply to this post by Boika Mileva
Hi Richard,

Besides of old accounting algorithms, there are other uses of rounding,
too. I use rounding (or truncating) often for purposes of categorization
of continuous variables. E.g.

GET FILE='C:\Program Files\SPSS\GSS93 subset.sav'.
temporary.
compute age = rnd(age / 5) * 5.
GRAPH /BAR(STACK)=COUNT BY age BY marital .

In such cases, rounding is quicker and better readable than RECODE.

Greetings

Jan

-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of
Richard Ristow
Sent: Friday, January 12, 2007 4:30 PM
To: [hidden email]
Subject: Re: How can I round an expression to 2 decimal points?

At 02:54 AM 1/12/2007, Boika Mileva wrote:

>I have to calculate an expression into a variable with a FORMAT F6.2
>and I need it rounded to 2 decimal points.

At 03:14 AM 1/12/2007, Spousta Jan responded:

>Compute rounded2 = RND(varname*100) / 100.

which is, of course, how you do it - implemented particularly neatly by
Marta's elegant macro.

The other answer is, seriously consider whether you should do it at all.
If you use the F6.2 format, the value will display properly rounded.
Rounding the value isn't necessary; and it introduces a small error in
the value, if you're going to use it in later calculations.

I suggested this once to a poster who, it turned out, was implementing
an odd accounting algorithm, and needed to match its rounding rules
exactly. But if the rounding is just for display, it's generally better
not to.
Reply | Threaded
Open this post in threaded view
|

Re: Suppress extra quotes in Export to CSV

King Douglas
In reply to this post by Peck, Jon
Jon,

Do you ever rest?  Your suggestion was perfect--thanks!  For those who may have a similar need, I'm posting the SAVE TRANSLATE portion of my program below.

I *still* don't know why this works.  I set the data up such that the values were surrounded with double quotes.
e.g., "This value is surrounded by double quotes"

Note that QUALIFIERS in the SAVE TRANSLATE command is set to single quotes.  The CSV output file faithfully maintains a single set of double quotes.  Why this should be the case is over my head, regardless of your explanation.

SAVE TRANSLATE OUTFILE = 'C:\TEMP\small_example.csv'
  /TYPE=CSV
  /TEXTOPTIONS QUALIFIER = "'" DELIMITER = ','
  /MAP
  /REPLACE
  /DROP INDEXVAR.

Thanks again, Jon, for service above and beyond the call of duty,

King



> -----Original Message-----
> From: Peck, Jon [mailto:[hidden email]]
> Sent: Sunday, January 14, 2007 2:56 PM
> To: King Douglas; [hidden email]
> Subject: RE: [SPSSX-L] Suppress extra quotes in Export to CSV
>
>
> From the SAVE TRANSLATE help.  QUALIFIER is on the text options
> subcommand.
>
> QUALIFIER="char". Specifies the qualifier to use to enclose
> values that contain the delimiter character. The default is a
> double quote. The value must be a single character (single byte),
> enclosed in quotes (single or double quotes). Values that contain
> the qualifier character will also be enclosed by the qualifier
> and qualifiers within the value will be doubled.
>
> Otherwise, the output would be ambiguous.  If your text contains
> double quotes, try using single quotes as the delimiter.
>
> HTH,
> Jon Peck
>
> -----Original Message-----
> From: SPSSX(r) Discussion [mailto:[hidden email]] On
> Behalf Of King Douglas
> Sent: Sunday, January 14, 2007 11:49 AM
> To: [hidden email]
> Subject: [SPSSX-L] Suppress extra quotes in Export to CSV
>
> I have an SPSS data file with one case of five long string variables.
>
> If the long string variables are not enclosed in quotes and I
> SAVE TRANSLATE to a comma-delimited CSV file, the long string
> variables in the CSV file are simply separated by a comma, e.g.:
> long string 1,long string 2,long string 3, etc.
>
> If the long string variables are enclosed in double quotes (e.g.,
> "long string 1"), two additional sets of quotes are added in the
> CSV file, e.g.: """long string 1""","""long string 2""","""long
> string 3""", etc.
>
> What's going on?  I want to append a single set of double quotes
> in the first example (because I have some commas in my long
> strings), or suppress the extra quotes in the second example.
>
> Any ideas?
>
> Thanks,
>
> King Douglas
> American Airline Customer Research
>
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Richard Ristow
In reply to this post by Spousta Jan
At 03:30 AM 1/15/2007, Spousta Jan wrote:

>I use rounding (or truncating) often for purposes of categorization of
>continuous variables. E.g.
>
>GET FILE='C:\Program Files\SPSS\GSS93 subset.sav'.
>temporary.
>compute age = rnd(age / 5) * 5.
>GRAPH /BAR(STACK)=COUNT BY age BY marital .
>
>In such cases, rounding is quicker and better readable than RECODE.

Exactly. (There are tastes and styles. I'd use RND in this case, too,
but generally I'm a RECODE fan.)

To go a little farther: While there are ways to make mistakes either
way, there are fewer reasons to be suspicious of a rounding to an
integer, than of a rounding to a fraction.
. At least, the rounded integer value is exact
. Commonly, the rounded integer value is intended as categorical
(ordered), as in Jan's example. One can fear that a rounded fractional
value is intended for further computation. (Though rounding to a
fraction is sometimes needed, as in Boika Mileva's case.)

- If you're rounding to categorize a continuous variable, round to
integers, to avoid inexact-representation problems that can be
maddening. (Like two different bars in the graph for what you think are
the 'same' value.)
- Very, very rarely use a rounded value in subsequent computations. It
introduces an extra imprecision in the value; it can only hurt you,
sometimes badly.
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

realmolejon
In reply to this post by Boika Mileva
I'm not sure I understand but for me, it would be easy to just create first the variable with the format of F6.2.

NUMERIC newvar (F6.2).
EXECUTE.

COMPUTE newvar=oldvar1/oldvar2.
EXECUTE.

so whatever the formula used of how it was computed, the designated variable (newvar) should always store the result with decimal of 2.
Data Processor
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

David Marso
Administrator
FORMAT simply affects the display of results and has NOTHING to do with precision!
realmolejon wrote
I'm not sure I understand but for me, it would be easy to just create first the variable with the format of F6.2.

NUMERIC newvar (F6.2).
EXECUTE.

COMPUTE newvar=oldvar1/oldvar2.
EXECUTE.

so whatever the formula used of how it was computed, the designated variable (newvar) should always store the result with decimal of 2.
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: How can I round an expression to 2 decimal points?

Jarrod Teo-2
Hi realmolejon,

Can you explore compute, round function?

Warmest Regards
Jarrod Teo (Dorraj Oet)

>
> realmolejon wrote
> > I'm not sure I understand but for me, it would be easy to just create
> > first the variable with the format of F6.2.
> >
> > NUMERIC newvar (F6.2).
> > EXECUTE.
> >
> > COMPUTE newvar=oldvar1/oldvar2.
> > EXECUTE.
> >
> > so whatever the formula used of how it was computed, the designated
> > variable (newvar) should always store the result with decimal of 2.
>
>
>
>

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
12