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
> 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
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/