Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Display Dates as Text in a Given Format

    I'm writing an SCM command, and I like to inform users of when the intervention was measured between, partly so it's clear and also so if they can see if they did something wrong (wouldn't wanna say COVID-19 happened in 1920, after all).

    Let's use an example!

    Code:
    *Boring Data Management Stuff
    
    qui {
    u "https://github.com/ebenmichael/augsynth/raw/master/data-raw/kansas_longer2.dta", clear
    keep if year > = 1990
    
    g time = yq(year, qtr)
    
    
    format %tq time
    
    loc int_time: disp tq(2012q1)
    
    
    g treated = 1 if time > `int_time' & Fi == 20
    
    replace treated = 0 if mi(treated)
    
    keep Fi treated time gdp
    
    levelsof  Fi if treated == 1, l(mun)
    qui g relative = .
    cls
    foreach units of loc mun {
        
        qui su time if treated ==1 & Fi ==`units', mean
        
        qui replace relative = time - `r(min)' if Fi ==`units'
    }
    
    cls
    
    tempvar obs_count
    qbys Fi (time): g `obs_count' = _N
    qui su `obs_count'
    
    qui drop if `obs_count' < `r(max)'
    }
    
    xtset Fi time, q
    
    
    su time if treated
    disp "Treatment is measured from `r(min)' to `r(max)'"
    The message I display at the end is what my program spits out when describing the dates of the intervention units. However, it says "Treatment is measured from 209 to 230".

    Strictly speaking, I don't and likely shouldn't care about this; I know what it means, Stata knows what I/the users mean, and everything else proceeds accordingly. But, I don't like displaying the dates in the numerical format unless of course it's the year.

    How might I display the time variables, any time variables, in the manner the user's formatted them in? I know when you use xtset, you get the r(tsfmt) macro, so I suspect this is part of the fix, by how would I use this or another technique to get Stata to display

    "disp "Treatment is measured from 2012q2 to 2017q3"

  • #2
    There are at least 3 ways to do this. For your purpose, a display directive in the form of option 1 or 2 would be handy. The last of which can be used for generating variables.

    Code:
    . di %td td(21jan2022)
    21jan2022
    
    . local today : di %td td(21jan2022)
    
    . di "`today'"
    21jan2022
    
    . di strofreal(td(21jan2022), "%td")
    21jan2022

    Comment


    • #3
      Code:
      display "Treatment is measured from " %tq `r(min)' " to " %tq `r(max)'
      By the way, I take (mild) offense at the "Boring Data Management Stuff" line. It is a common attitude, I think, that data management is dull and boring and all the glamor is in fancy regression models and exotic statistical tests. But if you look at the problems that people run into and ask for help here on Statalist, a substantial fraction arise from bad data management. And I believe that, in turn, comes from people not paying enough attention to doing data management properly. Moreover, in my view, data management is anything but boring. In fact, almost every project presents new and interesting data management challenges. By contrast, in my view, again, if you've seen one regression, you've pretty much seen them all.

      Added: Crossed with #2.
      Last edited by Clyde Schechter; 21 Jan 2022, 13:58.

      Comment


      • #4
        Since you asked how to display the dates "in the manner the user's formatted them in" we can build on the advice from Leonardo Guizzetti and use the format assigned to the variable in the display command, demonstrated on made-up data using two quarterly variables with different formats assigned.
        Code:
        . local qdfmt1 : format qd1
        
        . quietly summarize qd1
        
        . display "Treatment is measured from " `qdfmt1' `r(min)' " to " `qdfmt1' `r(max)'
        Treatment is measured from 2012q2 to 2017q3
        
        . local qdfmt2 : format qd2
        
        . quietly summarize qd2
        
        . display "Followup is measured from " `qdfmt2' `r(min)' " to " `qdfmt2' `r(max)'
        Followup is measured from Q4 of 2017 to Q1 of 2020
        
        . macro list _qdfmt1 _qdfmt2
        _qdfmt1:        %tq
        _qdfmt2:        %tq!Qq_!o!f_CCYY

        Comment


        • #5
          Thanks so much, all these helped a lot!

          Clyde Schechter Oh I'm totally in your camp, I agree with you. That comment was more for myself honestly- this is the SCM dataset I'm doing lots of my testing with, so I find it boring because I'm tired of looking at it since it's my go-to example.

          You're right, lots of folks don't take data management seriously at all. Ever since I've become a contributor, lots of issues I've seen haven't been from statistical estimation (though that's certainly an issue), it's more been from fundamental things like "This variable isn't a numeric variable" or "you need to ensure the panel is balanced for this to make sense." For my Synthetic LASSO command, I've been reaching out to researchers who've recently published work in synthetic controls. And the datasets I've received have come in all shapes and sizes, some simple, some complex, and the thing I like about all of them is that they're all a unique challenge.

          And that's partly why I like being a researcher anyways, no two days are the same. Every day presents a distinct challenge; one day, you're testing the "single unit treated" case. The next you're doing placebos and graphing them correctly, the next you're devising syntax for multiple treatments and having to learn how to drop previously-treated units and so on. You don't get that from most jobs, so even though it's looked at by many as rudimentary, having your data "tidy" as R would want is pretty undervalued, much as making code and data public are, in my opinion.

          Comment

          Working...
          X