Announcement

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

  • Creation of a variable to identify year and month (time series data)

    Hi all. I have a time series dataset with information on political scandals involving the president in Argentina year per month, from 2014 to 2019 (N = 72).

    The variables identifying time are “year” and “month” (see below):

    I need to create a variable called “dm”, which identifies the year and month. For instance, 2014m1, 2014m2, 2014m3 … 2014m12.

    However, I’'ve been unable to do it.

    Could someone help?

    Thank you very much.


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str9 country str11 countrycode int year str10 month_name byte(month prescan2)
    "Argentina" "ARG" 2014 "January"    1 0
    "Argentina" "ARG" 2014 "February"   2 0
    "Argentina" "ARG" 2014 "March"      3 0
    "Argentina" "ARG" 2014 "April"      4 0
    "Argentina" "ARG" 2014 "May"        5 0
    "Argentina" "ARG" 2014 "June"       6 1
    "Argentina" "ARG" 2014 "July"       7 0
    "Argentina" "ARG" 2014 "August"     8 0
    "Argentina" "ARG" 2014 "September"  9 0
    "Argentina" "ARG" 2014 "October"   10 0
    "Argentina" "ARG" 2014 "November"  11 0
    "Argentina" "ARG" 2014 "December"  12 0
    end

  • #2
    Code:
    gen int dm = ym(year, month)
    format dm %tm
    If you will be working with date or time variables more than just this once, you are well advised to read the Datetime chapter in the [D] volume of the PDF manuals that are installed with your Stata. It's a long read and there is a lot there. But it will acquaint you with the philosopy of Stata's datetime variables, the functions available to create and transform them and calculate with them. You won't remember everything, but at the end you will probably be able to remember what functions are called for when the need for one arises, and then you can rely on the help files to refresh your memory about the details. The time you spend doing this will be amply and soon repaid.

    Comment


    • #3
      Thank you very much for the help, Clyde. I missed the "int" command when I was trying to generate the year/month variable. I will peruse the PDF manuals more thoroughly. Datetime variables are hard for me; I don't work with time series data very often.

      Comment


      • #4
        int is not a command. It's a variable type. Here it instructs generate to put the monthly date in an int type, which isn't essential but is more efficient storage than the usual default of float. From

        Code:
        help data types
        it can be seen that ints can be used to store integers between -32767 and 32740, which is more than sufficient than applications of monthly dates likely in Stata.

        For comparison, note

        Code:
        . di %tm -10000
         1126m9
        
        . di %tm 10000
         2793m5

        Comment


        • #5
          ... more than sufficient for ...

          Comment


          • #6
            Thanks for the clarification, Nick. I made a mistake: "int" is a variable type; you're right.

            Comment

            Working...
            X