Announcement

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

  • Generating date variable

    Hello all,

    I have the following code:

    Code:
    clear
    input float(qdate)
    1900q1
    1900q2
    1900q3
    1900q4
    end
    tempfile data
    save `data'
    I want to transform the output into a date variable that looks like this:

    Code:
    31mar1900
    30jun1900
    30sep1900
    31dec1900
    I would appreciate any assistance!

    Anoush K.

  • #2
    Well, if that is the code you have, first you have to fix it so it does anything useful at all! Here's what it does:
    Code:
    . clear
    
    . input float(qdate)
    
             qdate
      1. 1900q1
    '1900q1' cannot be read as a number
      1. 1900q2
    '1900q2' cannot be read as a number
      1. 1900q3
    '1900q3' cannot be read as a number
      1. 1900q4
    '1900q4' cannot be read as a number
      1. end
    
    . tempfile data
    
    . save `data'
    no variables defined
    The problem is that expressions like 1900q1 are not floats, they are strings. Now it is unclear to me what kind of data you are actually working with. Your actual data might be strings like this, or it might be real numeric date variables that are display-formatted to look like 1900q1 etc., but are really numbers like -240, -239, etc. I'm going to assume that you have the strings.

    So here is real -dataex- that produces a data set with a string variable qdate that looks like what you show:
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str6 qdate
    "1900q1"
    "1900q2"
    "1900q3"
    "1900q4"
    end
    So the first step is to make numeric quarterly dates out of these strings.
    Code:
    //  CONVERT TO GENUINE STATA INTERNAL FORMAT QUARTERLY DATES
    gen int _qdate = quarterly(qdate, "YQ"), after(qdate)
    assert missing(_qdate) == missing(qdate)
    format _qdate %tq
    drop qdate
    rename _qdate qdate
    des qdate
    Note the results given by -des qdate-: it is not a string, it is an int with %tq display format. It looks like 1900q1, etc., for your convenience in examining the data. But what's really there inside memory is a bunch of integers.

    Next step is to create a daily date corresponding to the end of the quarter. There is no Stata function that directly does this. But the function dofq() will take a quarterly date and return the first day of the quarter it is given. So the little trick is to give it the quarterly date corresponding to the next quarter and then back up one day:
    Code:
    //  CREATE A DAILY DATE CORRESPONDING TO THE LAST DAY OF THE QUARTER
    gen int end_of_quarter = dofq(qdate+1) - 1
    format end_of_quarter %td
    des end_of_quarter
    Notice, again that although it is formatted to look like 31mar1900, etc., it is in actuality a bunch of integers dressed up (%td format) to look that way for your convenience. You can do whatever daily date calculations you need with this variable and will get correct results.

    Code:
    . list
    
         +--------------------+
         |  qdate   end_of_~r |
         |--------------------|
      1. | 1900q1   31mar1900 |
      2. | 1900q2   30jun1900 |
      3. | 1900q3   30sep1900 |
      4. | 1900q4   31dec1900 |
         +--------------------+

    Comment


    • #3
      Thank you, Clyde Schechter. You are always so helpful!

      Anoush

      Comment

      Working...
      X