Announcement

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

  • How to generate a variable that takes value 1 if another variable is absent across all available rounds?

    Hi,

    Please consider the following example data

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str8 id float(round hi)
    "IN010001" 2 .
    "IN010001" 3 1
    "IN010001" 4 1
    "IN010001" 5 1
    "IN010002" 2 .
    "IN010002" 3 1
    "IN010002" 4 1
    "IN010002" 5 0
    "IN010003" 2 .
    "IN010003" 3 1
    "IN010003" 4 1
    "IN010003" 5 1
    "IN010004" 2 .
    "IN010004" 3 1
    "IN010004" 4 1
    "IN010004" 5 1
    "IN010005" 2 .
    "IN010005" 3 0
    "IN010005" 4 1
    "IN010005" 5 1
    "IN010006" 2 .
    "IN010006" 3 0
    "IN010007" 2 .
    "IN010007" 3 0
    "IN010007" 4 0
    "IN010007" 5 0
    "IN010008" 2 .
    "IN010008" 3 1
    "IN010008" 4 0
    "IN010008" 5 0
    "IN010009" 2 .
    "IN010009" 3 1
    "IN010009" 4 1
    "IN010009" 5 1
    "IN010010" 2 .
    "IN010010" 3 1
    "IN010010" 4 1
    "IN010010" 5 1
    "IN010011" 2 .
    "IN010011" 3 1
    "IN010011" 4 0
    "IN010011" 5 1
    "IN010012" 2 .
    "IN010012" 3 1
    "IN010012" 4 1
    "IN010012" 5 1
    "IN010013" 2 .
    "IN010013" 3 1
    "IN010013" 4 1
    "IN010013" 5 1
    "IN010014" 2 .
    "IN010014" 3 1
    "IN010014" 4 1
    "IN010014" 5 1
    "IN010015" 2 .
    "IN010015" 3 0
    "IN010015" 4 1
    "IN010015" 5 1
    "IN010016" 2 .
    "IN010016" 3 0
    "IN010016" 4 1
    "IN010016" 5 1
    "IN010017" 2 .
    "IN010017" 3 1
    "IN010017" 4 1
    "IN010017" 5 1
    "IN010018" 2 .
    "IN010018" 3 1
    "IN010018" 4 1
    "IN010018" 5 1
    "IN010019" 2 .
    "IN010019" 3 1
    "IN010019" 4 1
    "IN010019" 5 1
    "IN010020" 2 .
    "IN010020" 3 1
    "IN010020" 4 1
    "IN010020" 5 0
    "IN010021" 2 .
    "IN010021" 3 1
    "IN010021" 4 1
    "IN010021" 5 1
    "IN010023" 2 .
    "IN010023" 3 1
    "IN010023" 4 1
    "IN010023" 5 1
    "IN010024" 2 .
    "IN010024" 3 1
    "IN010024" 4 1
    "IN010024" 5 1
    "IN010025" 2 .
    "IN010025" 3 0
    "IN010025" 4 0
    "IN010025" 5 0
    "IN010026" 2 .
    "IN010026" 3 1
    "IN010026" 4 1
    "IN010026" 5 1
    "IN010027" 2 .
    "IN010027" 3 0
    end
    I want to generate a variable never_taker that takes the value 1 if hi is 0 across all three rounds, and takes the value 0 if hi is 1 in any of the three rounds.

    It would be great if someone could offer some suggestions.
    Thanks!

  • #2
    assuming that by "all 3 rounds" you mean rounds 3-5, the following should do it:
    Code:
    . bys id: egen byte rtot=total(hi) if inrange(round,3,5)
    
    . bys id: egen num=count(round) if inrange(round,3,5)
    
    . gen byte never_taker=rtot==0 & num==3

    Comment


    • #3
      As a small twist on @Rich Goldstein's idea consider say

      Code:
      bys id : egen count1 =total(hi & inrange(round(3, 5)))
      
      gen never_taker = count1 == 0
      Last edited by Nick Cox; 31 May 2022, 04:56.

      Comment


      • #4
        Originally posted by Rich Goldstein View Post
        assuming that by "all 3 rounds" you mean rounds 3-5, the following should do it:
        Code:
        . bys id: egen byte rtot=total(hi) if inrange(round,3,5)
        
        . bys id: egen num=count(round) if inrange(round,3,5)
        
        . gen byte never_taker=rtot==0 & num==3
        Thanks Rich. this was really helpful. Just one problem: when the value 1 appears for never_taker, it appears against rounds 3-5, but not against round 2. Say for eg, an ID "IN010026" is available for rounds 2,3,4,5. The variable "hi" takes 0 values across rounds 3-5. So never_taker would be 1. But this value 1 appears against rounds 3-5 but not against round 2. Value of never_taker for round 2 is still 0. Is there any way to modify the code such that if for any ID,
        Code:
        hi==0
        for rounds 3-5, against all observations of that ID, we have
        Code:
        never_taker==1
        ?

        Comment


        • #5
          Originally posted by Nick Cox View Post
          As a small twist on @Rich Goldstein's idea consider say

          Code:
          bys id : egen count1 =total(hi & inrange(round(3, 5)))
          
          gen never_taker = count1 == 0
          Thanks Nick for your answer. However, the first line of the code is producing an error:
          Code:
          bys id : egen count1 =total(hi & inrange(round(3, 5)))
          invalid syntax
          r(198);

          Comment


          • #6
            Sorry; should be

            Code:
             
             inrange(round, 3, 5)
            as in Rich Goldstein 's posts.

            Comment


            • #7
              Titir Bhattacharya I did not understand that you wanted the "1" on all occasions so use egen with the max option to get it for any missing founds; see
              Code:
              h egen
              the above will give you a new variable; here is a way without a new variable:
              Code:
              bys id (round): replace never_taker=never_taker[_n+1] if never_taker==.
              Last edited by Rich Goldstein; 31 May 2022, 13:57.

              Comment


              • #8
                Thanks Nick and Rich..Your solutions were really helpful

                Comment

                Working...
                X