Announcement Icon Online training class for Clinical R programming batch starts on Monday, 02Feb2026. Click here for details.

Apply Basic Numeric Functions


Lesson Description
-
  • Sometimes, we want to work with the concept of "Apply Basic Numeric Functions" in a clear, repeatable way.
  • This lesson walks through a simple example and shows the key steps.
  • We will see one approach on how we can do it in SAS and R.

 


data numbers; 
input num; 
round_val = round(num); 
ceil_val = ceil(num); 
floor_val = floor(num); 
int_val = int(num); 
abs_val = abs(num); 
mod_val = mod(num, 
3); 
datalines
-2.51 
-1.2 

1.3
2.7 

run;

 

mod_sas_style <- function(x, y) {
  x - y * trunc(x / y)
}

df <- tibble(
  num = c(-2.51, -1.2, 0, 1.3, 2.7)
) %>%
  mutate(
    round_val = round(num),
    ceil_val  = ceiling(num),
    floor_val = floor(num),
    int_val   = trunc(num),
    abs_val   = abs(num),

mod_val1   = num%%3, 
    mod_val2   = mod_sas_style(num, 3)  
  )
mod_sas_style <- function(x, y) {
  x - y * trunc(x / y)
}

df <- data.frame(
  num = c(-2.51, -1.2, 0, 1.3, 2.7)
)

df$round_val <- round(df$num)

df$ceil_val <- ceiling(df$num)

df$floor_val <- floor(df$num)

df$int_val <- trunc(df$num)

df$abs_val <- abs(df$num)

df$mod_val1 <- df$num %% 3

df$mod_val2 <- mod_sas_style(df$num, 3)
  • round/ceiling/floor/trunc/abs apply common numeric transforms.
  • %% returns a modulo; the custom function mimics SAS mod.