Announcement

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

  • Problem analyzing outcome by date

    Hello STATAListers,

    I'm a new user and having trouble working on a simple dataset with just 5 variables but only 3 are subject to this inquiry: "subject" (integers 1-100), the outcome "retired" (binary 0 or 1), and "date _born" (formatted as %tdnn/dd/CCYY).

    I am having trouble summarizing "retired" by "date_born."

    Using "List" I can confirm the subject data is all there and formatted consistently

    The error I have been receiving is "date: operator invalid"

    The commands I have been trying is:

    . summarize retired if date_born<=01/01/2001

    I've also tried a number of combinations where the date_born<2001 or date_born==[a specific mm/dd/YYY matching a subject's birthdate] but to no avail.

    I'd appreciate any help regarding how to format my date_born variable to execute the command properly

    Thanks!


  • #2
    Welcome to Statalist, and to Stata.

    Writing 01/01/2001 is not how you specify a Stata Internal Format daily date. Try
    Code:
    summarize retired if date_born<=td(01jan2001)
    or
    Code:
    summarize retired if year(date_born)<2001
    With that said, some advice.

    As a new user, you will find that Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata 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.

    And perhaps a little more genral advice.

    I'm sympathetic to you as a new user of Stata - it's a lot to absorb. And even worse if perhaps you are under pressure to produce some output quickly. Nevertheless, I'd like to encourage you to take a step back from your immediate tasks.

    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, and know how to find out more about them in the help files and PDF manuals.

    Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

    Comment


    • #3
      Thank You Mr. Lisowski,

      I've tried the two codes you kindly provided but I continue to recieve an r(198); error for "Date: operator invalid"

      I'm reading up on help datetime but am still having trouble identifying the issue due to the format listed as"%tdnn/dd/CCYY" in the properties of the "date_born" and that doesn't seem to match anything else listed in the document.

      Any ideas on perhaps how to reformat the dates to a different variable type instead of int?

      Comment


      • #4
        It is my bedtime now and perhaps someone will be able to advise you before I return.

        Any ideas on perhaps how to reformat the dates to a different variable type instead of int?
        This sentence serves only to reflect your lack of experience with Stata.

        You are guessing at what the solution might be without having established what is causing the problem. Your guessed solution makes no sense.

        You need to gain your understanding of Stata dates and times by first reading Chapter 24 of the Stata User's Guide PDF. The output of help datetime is just a reminder once you understand what you're doing, and it has links to other help files one of which explains in great detail what the output formats are.

        The first step toward a solutiion is to copy from your Stata Results window the commands you ran and the error message following it and post it here, because the error message you have received makes no sense (at this time of night, for me) for the command you have shown, and for that reason I ignored it when I wrote post #2 because I thought you had miscopied something. Show the original command and the two versions of my code. For good measure, post the output of the describe command as well.

        We can better help you if we know exactly what commands you have tried and exactly what Stata told you to indicate that there was a problem. Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. See especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

        Use the dataex command to provide a short sample of your data -
        Code:
        dataex in 1/10
        should be sufficient.

        Comment


        • #5
          In the example below, I attempt to reproduce the error you received by creating an int variable containing a SIF daily date and assigning it the display format %tdnn/dd/CCYY, and then using that variable in an if clause on a summarize command. The error does not occur. This suggests that the source of the error is not where you think it is, or that there is some other detail in what you are doing that is causing the problem. So to proceed further will require greater detail about your data and your code and its results, as suggested in post #4.

          Also, the output of help datetime display formats documents detailed display formats like yours. In the output of help datetime, it is the clickable link in the following passage, which appears just below the table of display formats.

          The display formats above are the simplest forms of each of the SIFs. You can control how each type of SIF date is displayed; see [D] datetime display formats.
          Code:
          . clear
          
          . set obs 10
          number of observations (_N) was 0, now 10
          
          . set seed 666
          
          . generate x = runiform()
          
          . format x %9.3f
          
          . generate int date_born = mdy(7,1,1995+_n)
          
          . format %tdnn/dd/CCYY date_born
          
          . list, clean noobs abbreviate(10)
          
                  x   date_born  
              0.150    7/1/1996  
              0.454    7/1/1997  
              0.276    7/1/1998  
              0.066    7/1/1999  
              0.398    7/1/2000  
              0.550    7/1/2001  
              0.267    7/1/2002  
              0.349    7/1/2003  
              0.818    7/1/2004  
              0.884    7/1/2005  
          
          . summarize x if date_born<=td(01jan2001)
          
              Variable |        Obs        Mean    Std. Dev.       Min        Max
          -------------+---------------------------------------------------------
                     x |          5    .2687425    .1628331    .066487   .4540368
          
          . summarize x if date_born<=01/01/2001
          
              Variable |        Obs        Mean    Std. Dev.       Min        Max
          -------------+---------------------------------------------------------
                     x |          0
          
          .

          Comment

          Working...
          X