Announcement

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

  • Set condition that variable takes value in a specified range

    I have one variable 'dur' showing the duration of treatments and another variable 'date' showing the end date of the treatment. I need to sum up the duration of treatments which have end dates between 1/1/2018 and 31/12/2018. How do I do this? I tried:

    Code:
    sum dur if date ==21185/21549
    ...and:

    Code:
    sum dur if 21185<date<21549
    ...without success.


  • #2
    See

    Code:
    help inrange()

    I need to sum up the duration of treatments which have end dates between 1/1/2018 and 31/12/2018. How do I do this? I tried:
    Note that sum is summarize and not taking the total, although you can access the sum from stored scalar -r(sum)-. If you want to add up values, use the -total()- function of egen.

    Code:
    help egen
    Last edited by Andrew Musau; 26 Jan 2022, 06:53.

    Comment


    • #3
      You can't simply say "without success". This is, as FAQ 12.1 implies, the equivalent of saying "X doesn't work." What does Stata return? What error if any, was issued? What do your data look like?

      If I understand you well, you're asking to see the treatment ranges of intervention units. Without seeing your data, I can only guess the issue. But, assuming you've got a "treated" variable that's = to 0 or 1, I would do something like

      Code:
      su date (if treated ==1) & inrange(date,21185,21549)

      Comment


      • #4
        To illustrate what I mean, we could do something like

        Code:
        import delim "https://raw.githubusercontent.com/synth-inference/synthdid/master/data/california_prop99.csv", clear
        
        su year if treated ==1
        with the only difference being is that you're asking about a specific date range too, which, as Andrew remarks, seems to be an issue the inrange function should solve.

        Comment


        • #5
          Jared, you are correct, to specifiy what I meant by "without sucess" Stata returned the average duration of treatment for 0 observations in the first code and for alle my observations in the second code. So "without sucess" meant, that it did not produce the desired output.

          The inrange command solved the problem, thanks. The command I needed was:

          Code:
          sum dur if inrange(date,21185,21549)

          Comment


          • #6
            Note that the command could be written more conveniently, and more transparently, as
            Code:
            sum dur if inrange(date,td(1/1/2018),td(31/12/2018))
            where the td() function is described in the output of help datetime .

            Comment


            • #7
              The question has been nicely answered several times over but I will add some footnotes.
              Code:
              sum dur if inrange(date, mdy(1, 1, 2018), mdy(12, 31, 2018))
              is another way to do it.

              Why should anyone care? One reason that may bite is that if you are used to a different date convention from Stata's default. As someone British I am more used to writing daily dates as 26 January 2022, or using a DMY convention. The virtue of the
              mdy() function is not that it allows this British convention -- because it doesn't -- but that the name itself mdy() is a vivid reminder of what order the arguments should arrive in.

              More pedantically,
              inrange() is a function, not a command. Stata is sharper than some other software in regarding functions and commands as different beasts.

              inrange() and its fraternal twin inlist() permit various tricks.

              Here's a personal favourite -- it was a major Doh! moment when I saw that it was possible and I had been failing to imagine it. I've heard that some others have had the same experience.
              Code:
              if inlist(1, a, b, c, d, e)
              is equivalent to
              Code:
              if (1 == a) | (1 == b) | (1 == c) | (1 == d) | (1 == e)
              which probably looks odd, but is naturally identical in meaning to
              Code:
              if (a == 1) | (b == 1) | (c == 1) | (d == 1) | (e == 1)
              which shouldn't look odd -- and is the kind of thing Stata users often want to write.

              My guess is that the indoctrination of mathematics teaching and reading stopped me seeing the equivalence immediately.

              While
              a == b surely is equivalent to b == a and we might write either in context, most of us would write a == 1 rather than 1 == a.

              More on functions -- including functions you may need now or soon -- at https://www.stata-journal.com/articl...article=dm0058

              Comment


              • #8
                In this particular case you can go straight to
                Code:
                 
                 sum dur if year(date) == 2018

                Comment


                • #9
                  Originally posted by William Lisowski View Post
                  Note that the command could be written more conveniently, and more transparently, as
                  Code:
                  sum dur if inrange(date,td(1/1/2018),td(31/12/2018))
                  where the td() function is described in the output of help datetime .
                  Thanks William, that is indeed a more convient and transparent way to write the code

                  Comment


                  • #10
                    Originally posted by Nick Cox View Post
                    In this particular case you can go straight to
                    Code:
                    sum dur if year(date) == 2018
                    Even more convenient, thanks Nick.

                    Comment

                    Working...
                    X