Announcement

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

  • Reshaping a time variable for a tsset command and a time graph

    Hello Stata community;

    I have this data at hand:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str7 mois float inflationen
    "1/1/20"    .8
    "1/2/20"    .6
    "1/3/20"    .6
    "1/4/20"    .2
    "1/5/20"    .1
    "1/6/20"    .1
    "1/7/20"    .2
    "1/8/20"    .1
    "1/9/20"   -.2
    "1/10/20"  -.6
    "1/11/20"   -1
    "1/12/20" -1.2
    "1/1/21"   -.7
    "1/2/21"   -.5
    "1/3/21"   -.4
    "1/4/21"  -1.1
    "1/5/21"   -.7
    "1/6/21"   -.4
    "1/7/21"   -.3
    "1/8/21"   -.4
    "1/9/21"    .2
    "1/10/21"   .1
    "1/11/21"   .6
    "1/12/21"   .8
    "1/1/22"    .5
    "1/2/22"    .9
    "1/3/22"   1.2
    "1/4/22"   2.4
    "1/5/22"   2.4
    "1/6/22"   2.3
    "1/7/22"   2.6
    "1/8/22"     3
    "1/9/22"     3
    "1/10/22"  3.8
    "1/11/22"  3.8
    "1/12/22"    4
    "1/1/23"   4.4
    "1/2/23"   3.3
    "1/3/23"   3.3
    "1/4/23"   3.5
    "1/5/23"   3.2
    "1/6/23"   3.3
    "1/7/23"   3.3
    "1/8/23"   3.1
    "1/9/23"     3
    "1/10/23"  3.3
    "1/11/23"  2.9
    "1/12/23"  2.6
    "1/1/24"   2.1
    "1/2/24"   2.8
    "1/3/24"   2.7
    "1/4/24"   2.5
    "1/5/24"   2.9
    "1/6/24"   2.9
    "1/7/24"   2.7
    "1/8/24"     3
    "1/9/24"   2.5
    end

    As you can see for the time variable called "mois", I do have it in the form of day/month/year. I do want to get rid of the day part (since it is always the first day of the month), I want to leave to month and the year part and destring them in such a way so that I could be able to apply a tsset command on that monthly time variable and draw a time graph showing the evolution of inflation.

    Any help please?

  • #2
    Code:
    * extract the last two digits of the year, add "20" to create a 4 digit year
    gen year = real("20" + substr(mois, -2, 2))   
    
    * extract the month 
    gen month = substr(mois, 3, .)
    replace month = substr(month, 1, strpos(month, "/") - 1)
    
    * convert the month from string to numeric format
    gen month_numeric = real(month)
    drop month
    * date variable in monthly format using the year and month
    gen date = ym(year, month_numeric)
    
    * apply the monthly date format 
    format date %tm
    tsset date
    
    * plot 
    tsline inflation
    Is this what you're looking for?

    Comment


    • #3
      Here's another way to get the monthly date.

      Code:
      . split mois, parse("/") destring 
      variables born as string: 
      mois1  mois2  mois3
      mois1: all characters numeric; replaced as byte
      mois2: all characters numeric; replaced as byte
      mois3: all characters numeric; replaced as byte
      
      . 
      . gen mdate = ym(2000 + mois3, mois2) 
      
      . format mdate %tm 
      
      . 
      . list mois mdate in 1/10 
      
           +-------------------+
           |    mois     mdate |
           |-------------------|
        1. |  1/1/20    2020m1 |
        2. |  1/2/20    2020m2 |
        3. |  1/3/20    2020m3 |
        4. |  1/4/20    2020m4 |
        5. |  1/5/20    2020m5 |
           |-------------------|
        6. |  1/6/20    2020m6 |
        7. |  1/7/20    2020m7 |
        8. |  1/8/20    2020m8 |
        9. |  1/9/20    2020m9 |
       10. | 1/10/20   2020m10 |
           +-------------------+

      Comment

      Working...
      X