Announcement

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

  • Bysort Trouble

    Hello,

    I am attempting to execute a very simple command where I create a new variable for each id (appearing many times in my data) corresponding to the first year that id appears in the data. My code is as follows:

    bysort id: egen(first_year)=min(year)

    Year is a numerical variable. However, I am getting the error "( invalid name, r;198". I have run similar commands in Stata countless times without any issues, so I am not sure what the problem is.

    I also tried
    by id: egen(first_year)=min(year)
    but was met with the same error.

    Any ideas?

    Thanks in advance,

    Erik

  • #2
    Stata is objecting to you specifying a variable name in parentheses.

    Try
    Code:
     bysort id: egen first_year=min(year)

    Comment


    • #3
      Note that you can simplify your code.
      Code:
      bysort id (year): generate first_year = year[1]
      The data will be sorted by id and within each id by year, then for each id the variable first_year will be set to the value of year in the first observation for that id, which will be the observation with the smallest value of year.

      Comment

      Working...
      X