Announcement

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

  • Labeling a date variable

    I observe the total number of sales for 48 months over 4 years in 4 different countries. I declared a panel data set after assigning a time id to each month. My code is:
    egen idtime=group(year month)
    xtset idcountry idtime.

    I want to do a basic scatterplot of sales over time. My code is
    scatter sales idtime
    However, on the x-axis I would like to see the exact month and year such as "2019 Jan" instead of artificially constructed idtime variable which goes as 1,2,...
    How can I do this? Thank you very much.

  • #2
    You would be better off with

    Code:
    gen mdate = ym(year, month)
    format mdate %tmCCYY_Mon
    xtset idcountry mdate
    scatter sales mdate

    Comment


    • #3
      As Nick implies, egen group() is not appropriate for combining a year and a month into anything useful as a date.

      Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

      All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      Comment


      • #4
        Thank you so much!

        Comment

        Working...
        X