Announcement

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

  • generating indicator variable for each id in a row

    Hello, I am interested in creating a variable "yes" that takes 1 for each "id" in "year" = 2002,2003,2004, 2005, 2006 and 0 otherwise.
    bys id: gen yes = 1 if year == 2002&2003&2004&2005&2006 --> this does not work.
    I cannot proceed with reshape command, as I have 10,000 observations.

    I would like variable "yes" that looks like:
    id year yes
    A 2002 1
    A 2003 1
    A 2004 1
    A 2005 1
    A 2006 1
    B 2003 0
    B 2005 0
    Anyone knows how to proceed?
    Thanks.

  • #2

    Never just say "does not work". Always say what happened and why it is not what you want. (FAQ Advice #12).

    But your problem can be solved.

    & is the wrong operator (and wrongly used).

    First consider that

    Code:
    if year == 2002 & year == 2003
    would mean that year is 2002 and also 2003, which is not what you want, for the same reason that the current year is not both 2018 and 2019.

    You want a variation on

    Code:
    if year == 2002 | year == 2003
    except that

    Code:
    gen yes = inrange(year, 2002, 2006)
    is neater.

    Notes:

    1. Why "wrongly used"? Because

    Code:
    year == 2002&2003&2004&2005&2006
    is parsed as

    Code:
    (year == 2002) & (2003) & (2004) & (2005) & (2006)
    which is a step further from what you want. (Stata won't fill in year == where you want it to go.)

    2, by: is irrelevant here, but will do no harm.

    Comment


    • #3
      Hello Nick, thank you for your reply and I will keep those in mind.
      Originally posted by Nick Cox View Post

      Code:
      gen yes = inrange(year, 2002, 2006)
      creates "yes=1" for "id = B" too.

      Comment


      • #4
        You are right that #1 states that B is unaffected. So, there is some extra rule you are not telling us.

        Comment

        Working...
        X