Announcement

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

  • Date variable

    Dear statalist members,

    I'm trying to use a date variable that I imported from an excel spreadsheet that is in the format dd/mm/yyyy, as it follows:
    01/01/1998
    01/02/1998
    ...
    01/11/2012
    01/12/2012
    As you can see I'm interested only in month and year, but when I tsset it I have the following result:


    Code:
    . tsset inst1 data, daily
           panel variable:  inst1 (unbalanced)
            time variable:  data, 01jan1998 to 01apr2012, but with gaps
                    delta:  1 day
    but I need it to have the delta to be monthly, but when I try to change it to monthly in the tsset it changes all my dates:

    Code:
    . tsset inst1 data, month
           panel variable:  inst1 (unbalanced)
            time variable:  data, 3116m9 to 3550m5
                    delta:  1 month
    Thank you all in advance.

    Gustavo

  • #2
    Try this:
    Code:
    gen data2 = mofd(data)
    format data2 %tm
    tsset inst1 data2, month
    But I think you will run into a problem because you seem to have multiple observations within the same month, so -tsset- will complain about that. Without knowing anything about your data or context, I will have to leave it to you to resolve that problem. In order to -tsset panel_variable date_variable- the panel and date variable must jointly identify unique observations. You say you want delta to be monthly, but your example data is clearly daily.

    Comment


    • #3
      I see no deep problem here, just small confusions.

      You have monthly data that just happen to bear daily date flags. As Clyde states, you should generate a monthly date variable and use that in tsset. It's pointless to declare a daily date variable as time variable because then there are gaps.

      But note that contrary to #1 Stata changed nothing that you did not change yourself.

      When your dates were read as daily dates then a numeric variable was created with values like

      Code:
      . di daily("01/01/1998", "DMY")
      13880
      
      . di daily("01/12/2012", "DMY")
      19328
      Stata's golden rule is that dates have origin the beginning of 1960. If you now yourself tell Stata that that numeric variable is really a monthly date, then Stata takes you at your word and tells you that you have monthly dates a few millennia into the future. Stata is fast but in this respect just a robot that follows instructions.

      Declaring a date variable to be something different is like changing a display format: Stata does not change your data, but it just interprets them exactly as instructed.

      Comment


      • #4
        Clyde the code you posted worked just fine! The data is monthly but, as Nick stated above, it had those daily date flags that were in the way. I do have multiple observations so I'm using the xtset now.

        Thank you both!

        Comment


        • #5
          On "doesn't work" as a poor error report please see FAQ Advice #12.

          This works for me:

          Code:
          . di daily("18.12.2014", "DMY")
          20075
          
          . di date("18.12.2014", "DMY")
          20075
          Tell us if those commands don't work for you. di means display.

          If not: Is date a string variable? Are you using Stata 15.1 or some older version?

          Comment


          • #6
            Please keep reading the FAQ Advice. I've already referred you to #12 which explains why screenshots are deprecated. #11 explains about telling us the version if out-of-date. In this case I don't think the version bites hard.

            Your dates in Stata (all we care about here) appear to be to be of the form MDY and must be converted as such. So, showing us 18.12.2014 was misleading


            Code:
            . di date("12.21.2014", "MDY")
            20078

            Comment


            • #7
              Again (three strikes and you're out!) please follow FAQ Advice to show us adequate materials easily understood.

              You want edate to show as dates like 20131217. So,

              These examples show that you need to assign the display format that will yield that.

              Code:
              . di %tdCYND mdy(12, 17, 2013)
              20131217
              
              . di %tdCYND 19709
              20131217

              Therefore

              Code:
              format edate %tdCYND
              How did I know that? I read help datetime which directed me to help datetime display formats and then read the details.



              Comment

              Working...
              X