I am trying to replace the read.SAS7bdat function with read_sas from the haven package in a number of my programs due to speed. Simply substituting it in works perfectly and reads so much quicker. However, the only hang-up I encounter has to deal with dates and times. For some reason, I can no longer subset by a date selected in an R Shiny date input even though the underlying data looks the same and all other functions work. If anyone knows of a difference between how these two functions read dates that would be greatly appreciated.
Asked
Active
Viewed 88 times
0
-
1Show an example of the generated R object from the two different reads and the R properties of the two different variable/object/column created from the SAS variable with date values. – Tom Jul 15 '20 at 15:15
-
Is the type of the date variable the same across your data sets? Is it Date, Num, Char? – Reeza Jul 15 '20 at 16:17
2 Answers
0
Date zero in SAS is 1 Jan 1960, and in R (origin date) it is 1 Jan 1970. That might be the reason for your issue. Bring in dates as character from SAS, and then convert it to numeric in R.
![](../../users/profiles/13333279.webp)
YBS
- 8,571
- 2
- 4
- 16
0
There were two components: first I had to change the origin date to 1970 instead of 1960 as I previously was using with read.sas7bdat. I also had previously converted everything to a POSIX date, which worked fine. However, subsetting by an R shiny date input wasn't working with read_sas so I converted the posix using as.date and this resolved it. Not exactly sure why though.
![](../../users/profiles/8615681.webp)
Nathan
- 11
- 2