Announcement

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

  • Converting string data variables

    Dear Community,

    I struggle with converting string date variables. I downloaded data on quarterly GDP from OECD in csv. The date is displayed as variable time e.g. : 2000-Q1 and it is string.
    What I wanna have is 2000q1 in double. I tried the command:

    gen t=date(time, "YQ")

    However Stata generated missing values denoted as: .

    My second idea was the command:

    gen t=qofd(quarterly(time,"YQ"))

    In this case Stata generated missing values as well.

    Please help me. I'm searching through the stata manuals but I'm not successful

  • #2
    Dear Guest,

    would be best see an example of your data by using -dataex-

    edit: Without looking at data I suggest something like this :
    Code:
    gen qdate = quarterly(time, "YQ")
    format qdate %tq
    Last edited by sladmin; 28 Aug 2023, 08:37. Reason: anonymize original poster

    Comment


    • #3
      Thank you so much Marco! I got the results

      Comment


      • #4
        Marco Errico is right but it remains to explain why #1 was wrong.

        date() is a function to produce daily dates, not a generic function to produce any kind of date. This is documented:

        Code:
        help date()
        The larger story runs that date() goes back to a time when daily dates were the only kind of date given specific support, yearly data being common but hardly difficult to handle.

        Later StataCorp added support for other kinds of dates and date() was matched by a synonymous function daily() but I guess the company thought that the date() function was so much used that they needed to keep documenting it, and it's never going to change its meaning.

        So, you can use daily() if so inclined but date() doesn't become anything else.



        The second guess in #1 started well, but if quarterly() worked then it produced quarterly dates, and qofd() doesn't fit because it takes daily dates and produces quarterly. But the result will not usually be missing.

        Let's trace this through. Here's a simple trick, but one I don't see people using when they're struggling.

        Use display on single, simple examples where you know what you start with and what you expect. If and when they work, scale up to generate.

        Code:
        . di quarterly("2000-Q1", "YQ")
        160
        It worked (i.e. no error message; no missing result). But what did it do? What is it as a quarterly date? Use a display format to find out.

        Code:
        . di %tq  quarterly("2000-Q1", "YQ")
        2000q1
        Bingo!

        Now this.
        Code:
        . di qofd(quarterly("2000-Q1", "YQ"))
        1
        It worked, i.e. no error message, no missing result, but what did it do?

        This is harder, but here goes. You told Stata that 160 is a daily date and asked for the corresponding quarterly date. And on a daily date scale where 0 = 1 January 1960, 160 is (was) a daily date in 1960 and is in the second quarter of 1960:

        Code:
        . di  %tq qofd(quarterly("2000-Q1", "YQ"))
        1960q2

        Comment


        • #5
          Thank you so much Nick for this thorough explanation!

          Comment

          Working...
          X