Search code examples
stata

Why is the incorrect date displaying in Stata


The local system datetime is 10:34 PM 1/8/2021.

In Stata I write

local datestamp: di %tdCCYY-NN-DD daily("S_DATE","DMY")
display `datestamp'    

and the output is 2012

If I write

di %tdCCYY-NN-DD daily("S_DATE","DMY")

I get 2021-01-08

Why the discrepancy? This is puzzling to me. I clearly assigned datestamp yet when I display it obviously something is wrong.


Solution

  • Executive summary: display saw 2021-01-08 and evaluated it as a expression in numbers. 2021 - 1 - 8 = 2012, so 2012 was what you saw.

    This is a subtle question, but the answer will show Stata's perfect logic, by its own rules.

    The code as posted in the question omits the crucial $ sign before S_DATE, which indicates a global macro, specifically a system macro containing the current daily date, obtained from your operating system.

    It is now 9 January 2021 in my time zone, but my example will work as well as yours to show what is going on. You defined a local macro, and then you included a reference to that local macro in a call to display. The display command has a designed inclination to calculate the result of any expression it sees before it displays the result of that calculation.

    Taking this more slowly: There are two quite distinct steps to the interpretation of your display command. First, as a matter of interpreting any Stata command line, all references to local and global macros are replaced with the contents of those macros (if they exist; it is not an error to refer to a macro that does not exist, but that is not an issue here). Second, display evaluates any expression it sees and then displays the result of that expression. Despite its name, display is not designed to show you directly any macro that exists, although that is what happens if the result of evaluating it leaves it the same as when it was presented. Thus if a local macro contains the string foo, that is what display will show you -- unless foo is the name of a scalar or variable, in which case the name won't be shown, just the values of that scalar or that variable (in the first observation, in the latter case).

    The command to see exactly what is inside a macro, without interpretation or calculation, is macro list.

    To the point, consider the different results here. In the first display command, the quotation marks " " are functional, not ornamental, and instruct display to treat its input as a string. Without the quotation marks, display is inclined to treat what it sees as numeric, and here it sees an expression, 2021 MINUS 1 MINUS 9, which evaluates to 2011. The leading zeros are ignored. In your case your date was 2021-01-08 and the result was 2012, as you reported.

    . local datestamp: di %tdCCYY-NN-DD daily("$S_DATE","DMY")
    
    . di "`datestamp'"
    2021-01-09
    
    . di `datestamp'
    2011
    

    You get the right answer with the last statement in your question. You fed display a number but instructed it to use a daily date display format to interpret that number, and you got exactly what you asked for and you expected. 22288 is, or was, 8 January 2021 on scale with origin 0 at 1 January 1960.