I'm reading in a sas data set using haven::read_sas
and I'm having an issue with columns that are supposed to be dates but are being read in as numeric.
Is there a way to specify when using read_sas()
to interpret column X
as a character or as a date? If it's helpful, the entries in the sas data set are of the form YYYYMM
so something like 202003
and read_sas
is taking them in as numeric.
Should mention that this date column is erroneously being read in (the numbers are messed up) so just modifying the column ex-post using as.Date
or as.character
does not work. For example, 202005
is being read in as 22036
.
CodePudding user response:
SAS uses January 1, 1960 as an origin date and all dates are a numeric value for days between the origin and the specified date. Any date prior to January 1, 1960 will be negative and any date after will be positive. After importing the data with haven
you can format it as a date using this origin.
x <- 22036
as.Date(x , origin = "1960-01-01")
#> [1] "2020-05-01"
For the SAS date documentation you can see here.