Announcement

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

  • Converting monthly string variable into monthly floating variable

    Hi guys,

    I have a feeling I'm not the first with this problem, but I just can not find anywhere on the internet the solution to my extremely time-consuming problem.

    So, I'm trying to analyze a dataset from 1962 until 2008 with monthly data. It is a string variable when I paste it into the data editor and it is a YMD format, which means day is 01 everytime.
    I've seen a youtube video from Stata how to convert a string variable in a floating variable and that works for me, but my problem is as follows:

    String variable starts January 1962 and goes on like that. Converting it to a floating variable 'gen t1 = date(t0, "YMD") results in stata listing the variable in days starting from 1960.
    (731 for Jan-1962, 762 for Feb-1962 etc). Now, that is not how I want my variable to look, so I try to convert that back to months. Instead of correctly converting it back to Jan/Feb/Mar 1962, it pretens like days are months. So Jan-1962 is 731, Feb-1962 is 762 in days, but it converts these days like they are months. In other words, suddenly my first time variable datapoint starts in december 2020, the second is july 2023 and so on and so on.

    I get it that stata has a system that counts days from 1960 onwards, but what I don't get is that it suddenly interprets these days as months when I want to convert the variable......

    Help me out please...

    (I need the data to be a suitable monthly time-series so that I can perform dfuller test. It won't do that at the moment because I've got a 'daily time series with gaps')

    Thanks in advance

  • #2
    Can you provide an example using dataex? see the FAQ for details

    Comment


    • #3
      You don't need to search the internet for solutions. They are supplied in the help. The help is, correspondingly, also on the internet, so

      http://www.stata.com/help.cgi?datetime

      and

      Code:
       
      help datetime
      get you to the same place.

      date() yields daily dates. To get the corresponding monthly dates use mofd() on daily dates.

      For example,

      Code:
      . display %tm mofd(date("2016 Aug 01", "YMD"))
       2016m8
      Conversion functions like mofd() are explained in the help.

      Comment


      • #4
        Thanks for your fast replies guys, but the reason I came to this forum is because the 'help guide' is no help to me, really... I can't even understand half of what is written there.

        Again, using 'help mofd' doesn't actually help me any further as to how to use this command. The command could solve my problem though, so could you explain to me how it works?

        Comment


        • #5
          I'm sympathetic to you as a new user of Stata - it's a lot to absorb.

          When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

          All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax.

          With regard to your current problem, before working with dates and times, any Stata user should read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF thoroughly. After that, the help datetime documentation will usually be enough to point the way.

          The Stata documentation is really exemplary - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry.

          Comment


          • #6
            As mentioned above, provide the first few dates from your dataset in their original form, that is, before you start manipuating them in Stata.

            Comment


            • #7
              The example already explains how mofd() works. If you take a date string and turn it into a numeric daily date then the mofd() function (not command) turns that result into a numeric monthly date.

              It seems that in your case you need something like

              Code:
              gen mdate  = mofd(t1) 
              format mdate %tm
              I'd recommend using evocative names like mdate. Names like t0 and t1 are harder to work with. Will you remember which is which?

              Comment


              • #8
                Thanks Nick! That is the solution I've been looking for

                Honestly, I couldn't understand the help from 'help mofd' but with your explanation I get it now. It worked and I've got my time series with a monthly delta now and I can perform the dfuller test which was my ultimate goal

                Thanks again!

                Comment

                Working...
                X