Announcement

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

  • Random dummy variable

    I had to setup a new dataset with 100 observations. Now, what is being asked is to create a dummy called prep for students who have taken a similar class before. The dummy should
    be equal to one for 25 of the students. I am a student, and we are struggling on this one. we'd be more than grateful for any help.
    Best,
    Catherine

  • #2
    This is an FAQ: http://www.stata.com/support/faqs/st...les/index.html

    Just shuffle them randomly then pick off the first (or the last) 25.

    Code:
    clear
    set obs 100
    set seed 2803
    gen id = _n
    gen random = runiform()
    sort random
    gen wanted = _n <= 25
    Note that using a condition like runiform() < 0.25 won't ensure exactly 25/100, but this technique will.

    Comment


    • #3
      Hi Catherine,

      I'm not exactly sure what you are asking for? How do you know which students have attended the class before? Or do you just want a variable called prep with 25 values of 1 and 75 values of 0? If the latter is the case, you can generate it like this:

      Code:
      clear
      set obs 100
      
      gen prep = inrange(_n, 1, 25)

      Comment


      • #4
        Catherine,

        This should work:

        Code:
        gen random = runiform()
        sort random
        gen dummy = 0
        replace dummy = 1 in 1/25
        Edit: Ok, Nick was faster ;-).

        Comment


        • #5
          thanks a lot guys! :-)

          Comment

          Working...
          X