Announcement

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

  • Selecting Date Range

    I have a data where one of the variables is CloseDate whose Variable Type is int and Format is %tdnn/dd/CCYY. I would simply like to select a range to include when Total the sum of a variable. In other words, sum the amounts between, for example, 1/1/2022 and 12/31/2022. As a new Stata user, I would appreciate help doing what I think should be an easy task. Thanks for your help.

  • #2
    Code:
    egen wanted = total(cond(inrange(CloseDate, td(1jan2022), td(31dec2022)), variable_to_be_summed, .))
    That's assuming you want to create a variable with the sum. If you just want to see the sum in your Results window it's a bit simpler:
    Code:
    summ variable_to_be_summarized if inrange(CloseDate, td(1jan2022), td(31dec2022)), meanonly
    display r(sum)
    If you want to create a variable, but do it separately by some grouping (e.g. separately by firm), then that's what Stata's -by:- prefix is for.

    Comment


    • #3
      Code:
      … if year(CloseDate) == 2022
      is one of several solutions.

      Comment


      • #4
        You'll be in a better position to handle this sort of coding if you take a little time to better familiarize yourself with how Stata deals with time.

        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 and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Comment


        • #5
          Thanks, William. I posted only after spending some time in the past days reviewing the available documentation.

          Comment


          • #6
            Clyde, thank you for your help. I have one last question. I tried using the by prefix to obtain totals by group. But the display r(sum) command only provides one total. How do I see the totals for each of my groups? Thanks in advance for your help.

            Comment


            • #7
              #6 is answered by Clyde Schechter 's first suggestion in #2.


              Code:
               
               egen wanted = total(cond(inrange(CloseDate, td(1jan2022), td(31dec2022)), variable_to_be_summed, .))
              where the total() does the summation and the cond() restricts the dates. More on this device in Section 9 of https://www.stata-journal.com/articl...article=dm0055

              Comment


              • #8
                I'm all set. I just used the following to obtain the totals I wanted:

                total Amount if inrange(CloseDate, td(1jan2022), td(31dec2022)), over(PgmCode)

                The trick, so to speak, was understanding the format for representing dates.

                Comment

                Working...
                X