Announcement

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

  • Year and month from cumulative number of months by group

    Dear all,

    I have a panel data with a large number of groups and the cumulative number of months (1 - 372) for January 1995 to December 2005. My goal is to extract the corresponding month and year for each observation.

    I used the following code;

    Code:
    gen startdate = "1975-01-01"
    gen enddate = "2005-12-31"
    
    gen start = date(startdate, "YMD")
    gen ep_end = date(enddate, "YMD")
    format start ep_end %td
    
    gen start_m = mofd(start)
    gen ep_end_m = mofd(ep_end)
    format start_m ep_end_m %tm
    
    gen index = month - 1
    gen current_month = start_m + index
    format current_month %tm
    I understand that there are probably less complicated ways of achieving this. The code seems to work and the current_month variable shows 1975m1 for cumulative month, however, when I try to extract the month and year from the variable, I get dates for the year 1960 and 1961. What am I doing wrong? Any help will be highly appreciated.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(id month) str10 startdate str11 enddate str6 start_m str7 ep_end_m
    1 1 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    1 2 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    1 3 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    2 1 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    2 2 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    2 3 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    3 1 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    3 2 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    3 3 "1-Jan-1975" "31-Dec-2005" "1975m1" "2005m12"
    end
    Sincerely,

    Chiara

  • #2
    If you want time to start in January 1975 you can do that but you're thereby declaring independence on dates.

    Stata's date functions and data formats are wedded to the idea that the date origin is the first possible date in 1960. So, for some purposes you need Stata monthly date variables and for other purposes you want your own time variable.

    The only way to mix the systems that I can see is to declare value labels for your own time variable that are monthly date formats.

    Comment


    • #3
      Dear Nick Cox, thank you for your reply. I apologize that I did not understand your suggestion.

      I am interested in converting the month number (1-372) to January 1975 to December 2005 by ID. Is that possible? Thanks again.

      Best regards,

      Chiara

      Comment


      • #4
        Here is some technique:

        Code:
        clear
        set obs 372
        gen current_month = _n
        
        * you start here
        
        forval t = 1/372 {
            local T = `t' + 179
            label def wanted `t' "`: display %tm `T''", modify
        }
        
        label val current_month wanted
        
        list in 1/12, sep(6)
        
            +----------+
             | curren~h |
             |----------|
          1. |   1975m1 |
          2. |   1975m2 |
          3. |   1975m3 |
          4. |   1975m4 |
          5. |   1975m5 |
          6. |   1975m6 |
             |----------|
          7. |   1975m7 |
          8. |   1975m8 |
          9. |   1975m9 |
         10. |  1975m10 |
         11. |  1975m11 |
         12. |  1975m12 |
             +----------+

        Comment

        Working...
        X