dbosak01 / libr

An R package to create data libraries and data dictionaries.
27 stars 1 forks source link

lib_load sas7bdat file date format #146

Closed akarito closed 1 year ago

akarito commented 1 year ago

When I load sas7bdat file using lib_load, the format of date was numeric,it is hard to re-format the date as format like yymmdd10. in sas.

dbosak01 commented 1 year ago

OK, let me look at it.

dbosak01 commented 1 year ago

I remember now. This is a problem with the underlying haven package. In SAS the date column is a numeric column with a format. But R can't understand those format names. So you have to explicitly tell the libname function which columns are dates. Like this:

library(libr)

# Create import spec and specify date data type for each column known to be a date
spcs <- specs(abc_adam_adlb = import_spec(TRTSDT = "date"))

# Pass import spec to libname
libname(dat, "c:/packages/Testing/data", "sas7bdat", import_spec = spcs )

# Load library
lib_load(dat)

# View dictionary to see data types
View(dictionary(dat.abc_adam_adlb))

Reminder that all dataset and column names are case-sensitive. If you can't get it, upload your dataset and I'll send you back some code to read it.

akarito commented 1 year ago

@dbosak01 Thanks! specs function can solve the format problem. I also use read_sas(path) function from haven package, it seems that the date format is correct.