Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add %tq date format #65

Open
JanMarvin opened this issue Sep 17, 2019 · 8 comments
Open

add %tq date format #65

JanMarvin opened this issue Sep 17, 2019 · 8 comments
Assignees

Comments

@JanMarvin
Copy link
Collaborator

A possible fix

diff --git a/R/read.R b/R/read.R
index b120e07..8926c73 100644
--- a/R/read.R
+++ b/R/read.R
@@ -341,6 +341,20 @@ read.dta13 <- function(file, convert.factors = TRUE, generate.factors=FALSE,
       z <- z - rowSums(outer(z, ls, ">="))
       as.POSIXct(z, origin = "1960-01-01")
     }
+    
+    convert_dt_q <- function(x) { 
+      z <- x / 4 
+      qrt <- z - floor(z)
+      z <- 1960 + floor(z)
+      
+      z[qrt == 0.00] <- paste0(z[qrt == 0.00], "-1-1")
+      z[qrt == 0.25] <- paste0(z[qrt == 0.25], "-4-1")
+      z[qrt == 0.50] <- paste0(z[qrt == 0.50], "-7-1")
+      z[qrt == 0.75] <- paste0(z[qrt == 0.75], "-10-1")
+      
+      z <- as.Date(z, "%Y-%m-%d")
+      z
+    }
 
     ff <- attr(data, "formats")
     ## dates <- grep("%-*d", ff)
@@ -359,6 +373,7 @@ read.dta13 <- function(file, convert.factors = TRUE, generate.factors=FALSE,
 
     for (v in grep("%tc", ff)) data[[v]] <- convert_dt_c(data[[v]])
     for (v in grep("%tC", ff)) data[[v]] <- convert_dt_C(data[[v]])
+    for (v in grep("%tq", ff)) data[[v]] <- convert_dt_q(data[[v]])
   }
 
   if (convert.factors) {
@JanMarvin JanMarvin self-assigned this Sep 17, 2019
@JanMarvin
Copy link
Collaborator Author

closed in b442419

@anjoung
Copy link

anjoung commented Nov 22, 2022

Hi! I am getting the following error message when I use your package:
dta3 <- read.dta13("edited/temp.dta") Warning messages: 1: In convert_dt_q(data[[v]]) : conversion of %tq failed 2: In convert_dt_q(data[[v]]) : conversion of %tq failed

Here's a link to a sample of the data I am working with which replicates the problem. The relevant variable is quarterly_dates I think that the package actually does effectively import quarterly dates in the test case, but since I am producing so many data files I cannot manually check each file every time this warning comes up. Would like to know the root cause and see if there is a fix!

@JanMarvin
Copy link
Collaborator Author

Maybe due to missing values in the date file? Could you have a look @sjewo ? I'm a bit swamped with other things ATM.

@JanMarvin
Copy link
Collaborator Author

Thanks for the report @anjoung , that's not supposed to throw a warning and looks like lazy coding.

@JanMarvin JanMarvin reopened this Nov 22, 2022
@sjewo
Copy link
Owner

sjewo commented Nov 22, 2022

Here's a link to a sample of the data I am working with which replicates the problem.

The file is not public. Could you provide another link?

@anjoung
Copy link

anjoung commented Nov 23, 2022

Sorry about that. Let me know if this link works: https://www.dropbox.com/s/3s90yn9hmcc6n7z/temp.dta?dl=0

@sjewo
Copy link
Owner

sjewo commented Nov 23, 2022

Thanks for the new link, @anjoung !

The warnings only occurs if NA values are present (e.g. in variable treat_quarter).

I posted a fix in 0a82b55.

Could you try the current testing branch?

library(remotes)
remotes::install_github("sjewo/readstata13", ref="testing")

@JanMarvin
Copy link
Collaborator Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants