Announcement

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

  • How to Randomly Select an individual using household id in a panel data?

    Hi,

    I am trying to randomly select an individual using the household id. Households have been interviewed in different rounds. Some have been interviewed in all four rounds while some have been interviewed in only 1 or 2 or 3. Based on this information, I am trying to do the following:

    Randomly selecting a person from every household that has been interviewed four times
    Randomly selecting a person from every household that has been interviewed three times
    Randomly selecting a person from every household that has been interviewed twice
    Randomly selecting a person from every household that has been interviewed once


    I have tried using tag by it does not work with the bysort condition. The same issue is with randomtag. I also tried using egen: max , and then based on this did runiform but it did not work for me. I have attached a sample of the data using dataex.



    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 hhid str1 memid str5 pid str6 visitmonth
    "1001" "1" "10011" "Mar-14"
    "1001" "1" "10011" "Sep-14"
    "1001" "1" "10011" "Jan-14"
    "1001" "2" "10012" "Feb-14"
    "1001" "2" "10012" "Mar-14"
    "1001" "2" "10012" "Apr-14"
    "1001" "2" "10012" "May-14"
    "1002" "1" "10021" "Aug-14"
    "1002" "1" "10021" "Jul-14"
    "1002" "2" "10022" "Feb-14"
    "1002" "2" "10022" "Mar-14"
    "1002" "2" "10022" "Apr-14"
    end

  • #2
    Originally posted by Jose Williams View Post
    Randomly selecting a person from every household that has been interviewed four times
    You can only select randomly if more than one individual fulfills a particular criterion. Otherwise if exactly one individual fulfills the criterion, the selection is not random. One way is to tag or isolate all individuals who fulfill the criterion and then generate a random variable. Sort using this random variable and then pick the individual sorted first within a household.

    Code:
    frame put *, into(wanted)
    frame wanted{
        bys hhid pid: keep if _N==4
        gen selector= rnormal()
        bys hhid selector: keep if pid==pid[1]
        list, sepby(hhid)
    }
    Last edited by Andrew Musau; 18 Aug 2022, 07:25.

    Comment

    Working...
    X