IBM Support

Ceiling, or "Round Upwards", function in SPSS Data Transformations

Troubleshooting


Problem

Is there a "ceiling" function in SPSS, i.e. a function that will transform noninteger numeric values upward to the next integer, regardless of the size of the noninteger portion? For example, both 4.2 and 4.7 would be rounded upwards to 5.0 by this function. If such a function exists, how does it handle negative numbers?

Resolving The Problem

There is no such function built into the SPSS transformation commands. However, it is easy to combine other functions to provide a ceiling operation.

Suppose that your active data file includes a variable, called XVAR, which you wish to round upwards. The following command will round XVAR upward to the next integer, if XVAR is noninteger, and store the result in the new variable XCEIL. Integer values of XVAR will simply be copied to XCEIL. Negative noninteger values of XVAR will be rounded to the larger integer, i.e. closer to 0. A value of -2.4 would be transformed to -2.0. The COMPUTE command below takes the integer portion of XVAR (TRUNC(xvar)) and adds a 1 if XVAR is both noninteger and positive, i.e., if the expression (xvar>TRUNC(xvar)) is true.

COMPUTE xceil = TRUNC(xvar) + (xvar > TRUNC(xvar)).
EXECUTE.

Suppose that you wanted numbers to be rounded away from 0, so that -3.4 was transformed to -4.0 and 2.3 was rounded up to 3.0. The following COMPUTE command would perform this function.

COMPUTE xceil = TRUNC(xvar) + (-1)**(xvar < 0)*(ABS(xvar) > TRUNC(ABS(xvar))).
EXECUTE.

The expression(-1)**(xvar < 0) raises -1 to the power of 1 if XVAR is negative, returning -1 so that 1 is subtracted from the integer portion of XVAR if XVAR is noninteger. If XVAR is
nonnegative, the expression raises -1 to the power of 0, returning 1, so that 1 is added to the integer portion of XVAR if XVAR is noninteger. The following pair of IF statements
perform the same function in a more straightforward manner.

IF (xvar >= 0) xceil = TRUNC(xvar) + (xvar > TRUNC(xvar)).
IF (xvar < 0) xceil = TRUNC(xvar) - (xvar < TRUNC(xvar)).
EXECUTE

[{"Product":{"code":"SSLVMB","label":"IBM SPSS Statistics"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"Not Applicable","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

17472

Document Information

Modified date:
16 April 2020

UID

swg21476167