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 has a particular value?

    Hi statalist,
    Please consider the following sample data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str5 placeid byte(schoolid schooltyp)
    "IN077"  4 1
    "IN077"  5 1
    "IN077"  6 1
    "IN077"  7 4
    "IN077"  8 1
    "IN078"  1 4
    "IN078"  2 4
    "IN078"  3 1
    "IN078"  4 1
    "IN079"  1 4
    "IN079"  2 4
    "IN079"  3 4
    "IN079"  4 1
    "IN079"  5 1
    "IN079"  6 1
    "IN079"  7 4
    "IN079"  8 1
    "IN079"  9 1
    "IN079" 10 1
    "IN080"  1 4
    "IN080"  2 4
    "IN080"  3 1
    "IN080"  4 4
    "IN080"  5 4
    "IN080"  6 1
    "IN080"  7 1
    "IN081"  1 1
    "IN081"  2 1
    "IN081"  3 1
    "IN081"  4 1
    "IN081"  5 4
    "IN081"  6 4
    "IN081"  7 4
    "IN081"  8 4
    "IN082"  1 1
    "IN082"  2 1
    "IN082"  3 1
    "IN082"  4 4
    "IN082"  5 1
    "IN082"  6 4
    "IN082"  7 1
    "IN082"  8 1
    "IN082"  9 1
    "IN083"  1 1
    "IN083"  2 4
    "IN083"  3 1
    "IN083"  4 1
    "IN083"  5 4
    "IN083"  6 4
    "IN083"  7 4
    "IN084"  1 1
    "IN084"  2 1
    "IN084"  3 4
    "IN084"  4 1
    "IN084"  5 4
    "IN084"  6 4
    "IN084"  7 4
    "IN084"  8 4
    "IN084"  9 4
    "IN085"  1 1
    "IN085"  2 1
    "IN085"  3 1
    "IN085"  4 4
    "IN090"  1 1
    "IN090"  2 4
    "IN090"  3 1
    "IN067"  4 4
    "IN067"  5 1
    "IN067"  6 1
    "IN067"  7 1
    "IN067"  8 1
    "IN067"  9 1
    "IN068"  1 4
    "IN068"  2 4
    "IN068"  3 4
    "IN068"  4 1
    "IN068"  5 1
    "IN068"  6 1
    "IN068"  7 1
    "IN069"  1 4
    "IN069"  2 4
    "IN069"  3 4
    "IN069"  4 1
    "IN069"  5 1
    "IN061"  1 4
    "IN061"  2 4
    "IN061"  3 4
    "IN061"  4 4
    "IN061"  5 4
    "IN061"  6 4
    "IN062"  1 4
    "IN062"  2 4
    "IN062"  3 4
    "IN062"  4 1
    "IN062"  5 1
    "IN063"  1 4
    "IN063"  2 4
    "IN063"  3 4
    "IN063"  4 1
    "IN063"  5 1
    end
    The data describes the list of schools present at a place. Each place, identified by placeid has multiple schools, of types 1 and 4. i want to generate a variable x which, for each placeid, takes the value 1 if schooltyp==1. If a placeid has at least 1 school with schooltyp 1 then x should take the value 1, otherwise 0.

    Would appreciate it if someone can help me out here.
    Thanks!

  • #2
    Code:
    by placeid, sort: egen x = max(schooltyp==1)

    Comment


    • #3
      Originally posted by Øyvind Snilsberg View Post
      Code:
      by placeid, sort: egen x = max(schooltyp==1)
      This worked perfectly. Thanks a lot

      Comment


      • #4
        This is a FAQ. https://www.stata.com/support/faqs/d...ble-recording/

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Thanks Nick. I'll be sure to check this out

          Comment

          Working...
          X