Announcement

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

  • Transforming time periods into variables non-string variables in order to apply the "tsset" command

    Hello Stata users;

    I'm using Stata versions 13.1, and I have this small data at hand:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 priode float tauxdecroissance
    "2005-2010"  .4
    "2010-2015" .14
    "2015-2020"  .3
    "2020-2025" .33
    "2025-2030"  .2
    "2030-2035" .12
    "2035-2040"  .1
    "2040-2045" .04
    "2045-2050" -.1
    end
    The idea is to draw a time bar graph to descrive the evolution of the tauxdecroissance by those periods. Is there any way possible to transform the "priod" variable in a way that I could apply the tsset command on it and draw such graph? Any other solutions that get me the same result are welcome too!

    With many thanks!

  • #2
    Assuming that in the full data set all of the values of priode are 5-year intervals, and there are no gaps between them, (as is true of the example data) I would do it as:
    Code:
    gen start_year = substr(priode, 1, 4)
    destring start_year, replace
    tsset start_year, delta(5)

    Comment


    • #3
      Clyde Schechter answered your question nicely, but a bar chart need have nothing to do with tsset.

      First, the overlap between quinquennia is presunably spurious. Let me guess that 2005-2010 is really 2005-2009, and so forth, If you have a different guess, or better information, do use that.


      Code:
       * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str9 priode float tauxdecroissance
      "2005-2010"  .4
      "2010-2015" .14
      "2015-2020"  .3
      "2020-2025" .33
      "2025-2030"  .2
      "2030-2035" .12
      "2035-2040"  .1
      "2040-2045" .04
      "2045-2050" -.1
      end
      
      split priode, parse("-") destring
      gen periode = strofreal(priode1) + "-" + strofreal(priode2 - 1)
      
      egen x = group(periode), label
      gen where = cond(tauxdecroissance > 0, 12, 6)
      
      twoway bar tauxdecroissance x, barw(0.95) legend(off) ///
      || scatter tauxdecroissance x, ms(none) mlabel(tauxdecroissance) mlabvpos(where) ///
      xla(1/9, valuelabel noticks labsize(small)) xtitle("") ysc(r(-0.12 .))
      Click image for larger version

Name:	taux.png
Views:	1
Size:	43.8 KB
ID:	1779720

      Last edited by Nick Cox; 10 Jul 2025, 15:54.

      Comment


      • #4
        Nick Cox Thanks for the rmark about those periods. I do want to say that I found the raw data on the Eurostat source just like that, there was no monthly detailed data for those 5-year periods, but I guess that, by 2005-2010, it is meant starting from January 2005 to December 2010, but anyway, it is just a guess.

        Comment


        • #5
          That's 6 years.

          Comment

          Working...
          X