Announcement

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

  • Combining two time variables (Year & Period of year (month)) into one time variable which could be used as tsset for graphs

    Hello Stata community;

    I have this dataset:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year str3 period float unemploymentrate
    2005 "Jan" 5.2
    2005 "Feb" 5.2
    2005 "Mar" 5.1
    2005 "Apr" 5.1
    2005 "May"   5
    2005 "Jun" 4.9
    2005 "Jul" 4.9
    2005 "Aug" 4.9
    2005 "Sep" 4.9
    2005 "Oct" 4.9
    2005 "Nov" 4.9
    2005 "Dec" 4.8
    2006 "Jan" 4.8
    2006 "Feb" 4.7
    2006 "Mar" 4.7
    2006 "Apr" 4.7
    2006 "May" 4.7
    2006 "Jun" 4.8
    2006 "Jul" 4.8
    2006 "Aug" 4.8
    2006 "Sep" 4.7
    2006 "Oct" 4.7
    2006 "Nov" 4.6
    2006 "Dec" 4.5
    2007 "Jan" 4.4
    2007 "Feb" 4.4
    2007 "Mar" 4.4
    2007 "Apr" 4.4
    2007 "May" 4.4
    2007 "Jun" 4.5
    2007 "Jul" 4.5
    2007 "Aug" 4.6
    2007 "Sep" 4.7
    2007 "Oct" 4.8
    2007 "Nov" 4.8
    2007 "Dec" 4.8
    2008 "Jan" 4.8
    2008 "Feb" 4.8
    2008 "Mar" 4.9
    2008 "Apr" 4.9
    2008 "May"   5
    2008 "Jun" 5.1
    2008 "Jul" 5.2
    2008 "Aug" 5.4
    2008 "Sep" 5.5
    2008 "Oct" 5.8
    2008 "Nov" 6.2
    2008 "Dec" 6.6
    2009 "Jan"   7
    2009 "Feb" 7.4
    2009 "Mar" 7.7
    2009 "Apr"   8
    2009 "May" 8.1
    2009 "Jun" 8.2
    2009 "Jul" 8.2
    2009 "Aug" 8.2
    2009 "Sep" 8.2
    2009 "Oct" 8.2
    2009 "Nov" 8.2
    2009 "Dec" 8.3
    2010 "Jan" 8.4
    2010 "Feb" 8.5
    2010 "Mar" 8.5
    2010 "Apr" 8.4
    2010 "May" 8.3
    2010 "Jun" 8.1
    2010 "Jul"   8
    2010 "Aug"   8
    2010 "Sep"   8
    2010 "Oct"   8
    2010 "Nov" 7.9
    2010 "Dec" 7.9
    2011 "Jan" 7.8
    2011 "Feb" 7.7
    2011 "Mar" 7.6
    2011 "Apr" 7.6
    2011 "May" 7.7
    2011 "Jun" 7.8
    2011 "Jul" 7.8
    2011 "Aug" 7.9
    2011 "Sep" 7.9
    2011 "Oct" 7.8
    2011 "Nov" 7.7
    2011 "Dec" 7.6
    2012 "Jan" 7.5
    2012 "Feb" 7.5
    2012 "Mar" 7.5
    2012 "Apr" 7.6
    2012 "May" 7.7
    2012 "Jun" 7.7
    2012 "Jul" 7.8
    2012 "Aug" 7.8
    2012 "Sep" 7.7
    2012 "Oct" 7.7
    2012 "Nov" 7.7
    2012 "Dec" 7.7
    2013 "Jan" 7.6
    2013 "Feb" 7.5
    2013 "Mar" 7.4
    2013 "Apr" 7.3
    end
    My goal is to "combine" (if you wanna say so), is to combine the two time variables (year & period) into one time variable (we could call it date) which shows the month and the year as a one variable, rather than two variables separated.

    I would love to have this variable as a numeric variable 'or even a numeric-time variable) so when I apply the "tsset" command on it, it doesn't show that my variable is a string one and that a "tsset" command can'b be applied. At the end, my goal is to draw a time-line graph with the "unemployment" as a Y variable.

    I hope I could get some help, thanks very much.

  • #2
    Code:
    local i 1
    local lab
    foreach month in `c(Mons)'{
        local lab `"`lab' `i' "`month'""'
        local ++i
    }
    label def lab `lab'
    encode period, g(month) label(lab)
    g wanted= ym(year, month)
    format wanted %tm
    tsset wanted
    Res.:

    Code:
    . tsset wanted
            time variable:  wanted, 2005m1 to 2013m4
                    delta:  1 month

    Also (easier)

    Code:
    g wanted2= ym(year, month(date("01"+period+ string(year)), "DMY"))
    format wanted2 %tm
    Last edited by Andrew Musau; 17 Jul 2022, 03:25.

    Comment


    • #3
      Thanks very much Andrew Musau, that was helpful.

      Comment


      • #4
        Here is another way to do the first part, after which tsset can be applied as shown by Andrew Musau:


        Code:
        gen mdate = monthly(string(year) + period, "YM") 
        
        format mdate %tm

        Comment

        Working...
        X