Announcement

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

  • Converting string to time variable

    Hey, I have monthly data in the following format:

    1970M1
    1970M2
    1970M3
    ...
    2017M12


    How can I convert this into time variable?

    Many thanks in advance and any help will be appreciated.

  • #2
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 string_date
    "1970M1"
    "1970M2"
    "1970M3"
    end
    
    gen int stata_date = monthly(string_date, "YM")
    
    format stata_date %tm
    list, noobs clean
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      thank you very much, Clyde! also thanks for the advice about how to put the post properly!

      Comment

      Working...
      X