Announcement

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

  • How can I combine several if conditions into one?

    Exemplary data:

    Code:
    input id wave status
    1 1 0
    1 2 1
    2 1 0
    2 2 0
    3 1 1
    3 2 0
    4 1 1
    4 2 1
    end
    
    gen wealth = runiform()
    I have the following problem: My intention is to compute summary statistics for certain observations in my dataset using combined if-conditions. In order to do this I tried the following line of code:

    Code:
    sum wealth if (wave == 1 & status == 0) & (wave == 2 & status == 0)
    However, I get an empty table:

    Code:
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
          wealth |          0
    Moreover, I get the same result changing the status variable. What am I missing here? For the above mentioned code snippet, I expected to get the summary table of wealth for the observation with id number 2. How can this be computed?
    Last edited by Steffen Mauch; 18 Jan 2022, 05:02.

  • #2
    Originally posted by Steffen Mauch View Post
    What am I missing here?
    You might want to review your Boolean algebra. In the meantime, try something like
    Code:
    summarize wealth if (wave == 1 & status == 0) | (wave == 2 & status == 0)

    Comment


    • #3
      'cause no observation can fullfill your condition: it is 1 (wave==1), and simultaneously, it is 2 (wave==2).
      you can use -tabstat- to get summary of wealth of different groups:
      Code:
      tabstat welath, by(wave)

      Comment


      • #4
        By the way, you'll get more than for ID 2, inasmuch as an observation each for IDs 1 and 3 also satisfy the conditions.

        Comment


        • #5
          Thanks for your answer. However, the summarize command now includes 4 observations. Besides the 2 observations with id number 2, also the first wave observation with id number 1 and the second wave observation with id number 3, right? Note, however that my if-conditions are supposed to isolate the 2 observations with id number 2. The summarize command is intended to summarize all observations that have status=0 during wave 1 AND status=0 in wave 2, which is only the case for id 2.

          Comment


          • #6

            Code:
            egen status1 = mean(cond(wave == 1, status, .)), by(id)
            egen status2 = mean(cond(wave == 2, status, .)), by(id 
            
            gen byte wanted = status1 == 0 & status2 == 0
            OR

            Code:
            bysort id (wave) : gen byte wanted = status[1] == 0 & status[2] == 0 if _N == 2
            Identifiers who appear in one wave only will be missing on the second code solution.

            See also https://www.stata-journal.com/articl...article=dm0055 for general trickery in this territory.

            Comment


            • #7
              That's exactly what I want! Thanks for your help and for the reference, Nick!

              Comment

              Working...
              X