BookmarkSubscribeRSS Feed
intersoldi
New User | Level 1

I need to recreate the code from SAS in R but I am Struggling to get the same results. I can get some rows to be the same but at the same time some other rows are different.

 

Can anyone help with creating R-code equivalent to the SAS code below?

data data_new;  
set data_old;  
months=intck('day30.5',date1,date2);  
run;

I've tried this with no luck (month2_sas is the sas result):

 

 
test_data <- data.frame(
  months2_sas = c(5, -2, -8, 10, 4, 7, -8, -7, -2, 2, 7, -7, -8, -11, -9, -8, 0, 11, 0, -2, -1, 7, -4, 7, -6, 10, 6, 9),
  date1 = as.Date(c("2021-06-16","2021-06-16","2020-09-23","2020-09-23","2020-09-23","2022-05-05","2022-03-10","2022-03-10","2022-03-10","2022-03-10","2022-03-10","2022-03-16","2020-02-04","2017-10-28","2017-10-28","2017-10-28","2017-10-28","2017-10-28","2017-10-28","2017-10-28","2017-10-28","2017-10-28","2017-04-28","2017-04-28","2020-10-31","2020-10-31","2023-06-22","2023-06-22")),
  date2 = as.Date(c("2021-12-03","2021-05-05","2020-02-10","2021-08-11","2021-02-09","2022-11-15","2021-08-02","2021-09-07","2022-02-02","2022-06-07","2022-10-31","2021-08-30","2019-06-21","2016-11-08","2017-01-06","2017-02-10","2017-10-02","2018-09-03","2017-10-10","2017-08-08","2017-09-04","2018-04-30","2016-12-09","2017-11-09","2020-05-18","2021-08-13","2023-12-05","2024-03-05"))
)

sas_epoch <- as.Date("1960-01-01")

test_data %>%
  mutate(   
    months_r = floor(as.numeric(date2 - sas_epoch) / 30.5) - 
      floor(as.numeric(date1 - sas_epoch) / 30.5)
  ) %>%
  select(  months2_sas,months_r)

 

2 REPLIES 2
Patrick
Opal | Level 21

Not knowing much about R syntax, what Copilot tells me sounds very convincing.

Patrick_0-1781175421157.png

I'm not going to post more AI answers I can't validate, but I'd suggest you use an AI, provide the SAS code and ask it to replicate this in R.

Quentin
Super User

This is an exact duplicate of a stack overflow question from a month ago which has answers there:
https://stackoverflow.com/questions/79933077/difference-between-dates-in-sas-doesnt-match-results

 

The Boston Area SAS Users Group is hosting free webinars!

Register now at https://www.basug.org/events.