Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help with ROUNDING VALUES in SPSS.

14,781 views
Skip to first unread message

M. Justice

unread,
Mar 14, 2001, 6:23:38 PM3/14/01
to
Hi,

I was wondering if anyone had any advice or suggestions on how to
successfully round up/down computed values within SPSS. Although I
recognize that SPSS allows for various arithmetic functions within the
COMPUTE statement (including RND, example below), this only allows for
rounding to the nearest integer. Unfortunately, this doesn't help me much
because I am looking to round values up to the nearest .5. For example, I
wish to somehow run a compute command which will change values of 1.33 to
1.5 or a value of 1.67 to 2.0. Are there further specifications to the
COMPUTE - RND syntax below which will allow this? What I am attempting to
avoid is creating a series of IF statements which capture values > and < in
order to convert these to the nearest .5 (b/c of the number of variables and
values in the data set).

COMPUTE variable2 = RND(variable1*2)/2 .
or alternatively,
COMPUTE var2 = .5*(rnd(2*var1)) .

create a range where values:

1.00 to 1.24 = 1.0
1.25 to 1.74 = 1.5
1.75 to 1.99 = 2.0

Are there any suggestions on how to get values such as 1.67 to round up to
2.0 rather than round down to 1.5? Mathematically the method seems
"sound," however, I can't help but feel that, subjectively, 1.67 should be
2.0 rather than 1.5. I'm not sure if anyone else agrees or what the exact
"stand" on rounding really is. .

Any help would be greatly appreciated.

Thanks in advance,

M. Justice


Rich Ulrich

unread,
Mar 15, 2001, 10:30:33 AM3/15/01
to
On Wed, 14 Mar 2001 23:23:38 GMT, "M. Justice" <cri...@hotmail.com>
wrote:

> Hi,
>
> I was wondering if anyone had any advice or suggestions on how to
> successfully round up/down computed values within SPSS. Although I
> recognize that SPSS allows for various arithmetic functions within the
> COMPUTE statement (including RND, example below), this only allows for
> rounding to the nearest integer. Unfortunately, this doesn't help me much
> because I am looking to round values up to the nearest .5. For example, I
> wish to somehow run a compute command which will change values of 1.33 to
> 1.5 or a value of 1.67 to 2.0. Are there further specifications to the
> COMPUTE - RND syntax below which will allow this? What I am attempting to
> avoid is creating a series of IF statements which capture values > and < in
> order to convert these to the nearest .5 (b/c of the number of variables and
> values in the data set).
>
> COMPUTE variable2 = RND(variable1*2)/2 .
> or alternatively,
> COMPUTE var2 = .5*(rnd(2*var1)) .
>
> create a range where values:
>
> 1.00 to 1.24 = 1.0
> 1.25 to 1.74 = 1.5
> 1.75 to 1.99 = 2.0

- This looks good. I think this is where you should start.

>
> Are there any suggestions on how to get values such as 1.67 to round up to
> 2.0 rather than round down to 1.5? Mathematically the method seems
> "sound," however, I can't help but feel that, subjectively, 1.67 should be
> 2.0 rather than 1.5. I'm not sure if anyone else agrees or what the exact
> "stand" on rounding really is. .

? Mathematically, rounding "up" is okay, and you can do that by
adding .4999999999 to each number, before using your formula.

I don't share your subjective feelings about 1.67. Are you proposing
1/3's as cutoffs? - so that 2/3 of all values get rounded to
integers?

You have more flexibility if you start with the commands that Bruce
shows you, for breaking a number into integer and decimal portions.
I think you can use "RECODE" on the "Decimal" variable, instead of
multiple IFs, before you add it back.

--
Rich Ulrich, wpi...@pitt.edu
http://www.pitt.edu/~wpilib/index.html

Bruce Weaver

unread,
Mar 15, 2001, 9:04:47 AM3/15/01
to M. Justice

Try this:

* Code by Bruce Weaver, wea...@mcmaster.ca, 15-Mar-2001
* Generate some data .
new file.
input program.
loop #i = 1 to 150.
compute case = $casenum.
if (case = 1) x = 1.
if (case > 1) x = lag(x) + .01.
end case.
end loop.
end file.
end input program.
exe.

* Compute new variable Y, which is X rounded to the nearest .5 .
* Use modulus function to strip off the decimal part of each number .

compute xmod1 = mod(x,1).
do if (xmod1 < .25).
compute whole = trunc(x).
compute decimal = 0.
else if (xmod1 < .75).
compute whole = truc(x).
compute decimal = .5.
else. /* xmod1 in GE .75 */
compute whole = trunc(x) + 1.
compute decimal = 0.
end if.
compute Y = whole + decimal.
exe.

formats case (f5.0) / x to y (f8.3).

list case x xmod1 whole decimal y.

* Note that when X = 2.25, Y = 2.00 rather than 2.50 .
* When I look at XMOD1 in the data file, the value is
actually .249999999 rather than .25 .

* To get around this, change the computation of XMOD1 as follows .

compute xmod1 = trunc(rnd(100*mod(x,1)))/100.
exe.

* Then repeat computation of whole, decimal, and Y.

do if (xmod1 < .25).
compute whole = trunc(x).
compute decimal = 0.
else if (xmod1 < .75).
compute whole = truc(x).
compute decimal = .5.
else. /* xmod1 in GE .75 */
compute whole = trunc(x) + 1.
compute decimal = 0.
end if.
compute Y = whole + decimal.
exe.

list case x xmod1 whole decimal y.
--
Bruce Weaver
New e-mail: wea...@mcmaster.ca (formerly wea...@fhs.csu.mcmaster.ca)
Homepage: http://www.angelfire.com/wv/bwhomedir/

pchw...@gmail.com

unread,
May 6, 2016, 10:06:51 AM5/6/16
to
SPSS RND Function
Rounding numbers in SPSS is done with the RND function. RND takes an optional second argument, which is the nearest value to round to. You can round to the nearest quarter point (second example below) or tenfold (third example).
If the second argument is omitted, values will be rounded to the nearest integer (first example).

http://www.spss-tutorials.com/spss-main-numeric-functions/

David Marso

unread,
May 7, 2016, 11:57:11 PM5/7/16
to
I suspect that 15 years later OP has probably worked out the solution or changed career path? A bit late to the party!

rohit...@gmail.com

unread,
Jan 28, 2019, 1:49:16 PM1/28/19
to
Yeah , but this helped me in 2019 :)

zarrin mehdizadeh

unread,
Jun 28, 2023, 9:08:17 AM6/28/23
to
On Monday, January 28, 2019 at 7:49:16 PM UTC+1, rohit...@gmail.com wrote:
> Yeah , but this helped me in 2019 :)
an this helped me in 2023! Thank you ! :)
0 new messages