8

I loaded a sas7bdat file using the sas7bdat package, but the dates are converted to a num like this:

sas <- c(16922, 17045, 17014, 16983)

I tried

rPOSIX <- as.POSIXct(sas,origin='1960-01-01')

as mentioned here but it's wrong. I don't have access to SAS but the dates should be around the year 2006.

Community
  • 1
  • 1
spore234
  • 3,550
  • 6
  • 50
  • 76
  • 2
    `as.Date(sas, origin = "1960-01-01")` this works well. – SabDeM Sep 14 '15 at 12:25
  • 1
    POSIXct stores dates as the number of seconds since the origin. The SAS dates are the number of days since the origin. This is the source of discrepancy. You may use either `as.Date`, or use `as.POSIXct` with `sas * 3600` (3600 seconds per day). Personally, I'd use `as.Date` since your SAS dates aren't specific to the second. – Benjamin Sep 14 '15 at 12:25
  • @SabDeM +Benjamin It's working, thanks – spore234 Sep 14 '15 at 12:28
  • 1
    3600 seconds per day? That's a rather short day... try 86400 seconds for an earth-day... – Joe Sep 14 '15 at 15:19
  • I found something different. Please see https://stackoverflow.com/questions/67495487/sas7bdat-date-format-is-stored-as-a-number?noredirect=1#comment119321404_67495487 – phargart May 12 '21 at 17:16
  • Related post as well: https://stackoverflow.com/questions/30006822/read-sas-sas7bdat-data-into-r – phargart May 12 '21 at 17:16

2 Answers2

11

As my previous comment, here is a working example, where the origin argument is set like that because of a specific SAS setting, which sets the origin date to 1960-01-01, here informations:

 as.Date(sas, origin = "1960-01-01")
[1] "2006-05-01" "2006-09-01" "2006-08-01" "2006-07-01"
SabDeM
  • 7,050
  • 2
  • 25
  • 38
  • I have an additional question, maybe you can help me: In my R data there is a column for the quarters, but its range is from 6-19 (not 1-4 as you might guess), how can I convert this format? I have no clue how this is calculated and don't know the original sas data. – spore234 Sep 14 '15 at 13:27
  • @spore234 I am sorry but I can't help you, maybe it is a sort of SAS specific (like the `origin` in my example), try to ask a new question and do not forget to put some data to play with because I have some ideas, maybe with the `lubridate` package. – SabDeM Sep 14 '15 at 13:51
0

You can try the sasdates package from R cran

Mahesh D
  • 29
  • 7
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 16 '22 at 23:28