Announcement

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

  • Problems with dates

    Hello Community,

    I'm quite unexperienced with STATA and right now standing infront of my first major problem.

    I want to plot a specific expenditure of OECD countries due to Covid-19. The data for this variable is uploaded every three mons (starting June2020, ending September2021).

    I created a variable (date) with the format YYYYMM, therefore starting with 202006 and ending with 202109. my problem is that even with the commands xtset and tsset STATA does not understand the format of the date.

    I tried to change the format into YYYYMMDD (adding a 01 to every observation) and when this didn't work, even to change this YYYYMMDD into a string variable (DateYMD) and destring it back with the command


    generate date2 = date(dateYMD, "YMD")



    The result is .... well, look at it yourselves
    date YM dateYMD dateYMD2
    202006 20200601 22067
    202010 20201001 22189
    202012 20201201 22250
    202103 20210301 22340
    202106 20210601 22432
    202109 20210901 22524

    What can I do? And if it's possible, explain it to me please because I want to understand what went wrong here!

    Thank you very much in advance and kind regards

    Vincent

  • #2
    The function date() has one and only one role, to create a daily date from string ingredient or ingredients. This is documented e.g. in help datetime or more formally at help date()

    You have monthly dates and so daily dates are not needed for any purpose you explain and indeed might not be useful in any case.

    If you have created a integer variable with run-together year and month, this is of limited use as at best Stata can only see a variable with (in principle) 11 possible steps of 1 and 1 possible step of 89, so that e.g. the jump from 202012 to 202101 is a jump of 89. A string variable is not much more helpful.

    Your example is helpful but stops short of being clear whether your variables are numeric or string.

    See https://www.statalist.org/forums/help#stata for advice on using dataex -- use of which is especially crucial for dates.

    Run-together monthly dates are awkward for Stata. You need to separate month and year components, and the way to do that depends on whether the variable is numeric or string.

    Here is some technique.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input long dateYM_num str6 dateYM_str
    202006 "202006"
    202010 "202010"
    202012 "202012"
    202103 "202103"
    202106 "202106"
    202109 "202109"
    end
    
    gen wanted1 = ym(floor(dateYM_num/100), mod(dateYM_num, 100))
    
    gen wanted2 = monthly(substr(dateYM_str, 1, 4) + " " + substr(dateYM_str, -2, 2), "YM")
    
    list, sep(0)
    
    format wanted? %tm 
    
    list, sep(0)
    Results of listings:

    Code:
     
    
         +-----------------------------------------+
         | dateYM~m   dateYM~r   wanted1   wanted2 |
         |-----------------------------------------|
      1. |   202006     202006       725       725 |
      2. |   202010     202010       729       729 |
      3. |   202012     202012       731       731 |
      4. |   202103     202103       734       734 |
      5. |   202106     202106       737       737 |
      6. |   202109     202109       740       740 |
         +-----------------------------------------+
    
         +-----------------------------------------+
         | dateYM~m   dateYM~r   wanted1   wanted2 |
         |-----------------------------------------|
      1. |   202006     202006    2020m6    2020m6 |
      2. |   202010     202010   2020m10   2020m10 |
      3. |   202012     202012   2020m12   2020m12 |
      4. |   202103     202103    2021m3    2021m3 |
      5. |   202106     202106    2021m6    2021m6 |
      6. |   202109     202109    2021m9    2021m9 |
         +-----------------------------------------+
    See also

    Code:
    help datetime display formats 
    for how to get a different display format other than the default %tm.

    Comment


    • #3
      Thank you very much, Mr.Cox. the function %ts enlighted me.

      Best regards,

      Vincent

      Comment

      Working...
      X