Announcement

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

  • How to keep observations with several criteria

    Dear Stata Community,
    I have the following problem:
    I have a panel data on the careers of students from a University. For each student I have the year of enrollment, the year of course (1st year, 2nd year, 3d year student), the identification for the exam held, the data in which the exam is taken with the grade and the corresponding number of credits, the degree course etc.

    I want to keep observations for which I have the same exam (id_exam) in the same year course in all the years.
    For example: I want to keep the exam with id=20 only if it is an exam of the first year (course year=1) both in the exam_year=2019 and the exam_year=2020.

    How can I do?


  • #2
    I'm trying to think about that..I need something like:

    egen ok=group(id_exam course_year degree_course)
    keep if (ok==i & exam_year==t) & (ok==i & exam_year==t+1)

    Comment


    • #3
      Hi Corinna, it's a bit difficult to tell if your attempt is all-encompassing, without having the data (you might consider making an example data command like in many of the other posts). How about something like
      Code:
      gen ok1=.
      egen ok=group(id_exam course_year degree_course)
      qui sum ok
      local j=r(max)
      sort ok exam_year
      forvalues i=1/`j' {
           replace ok1=1 if ok==`i' & exam_year[_n]==(exam_year[_n+1]-1)
           }
      keep if ok1==1

      Comment


      • #4
        keep if (ok==i) & ((exam_year==t) | (exam_year==t+1))

        Originally posted by Corinna De Leo View Post
        I'm trying to think about that..I need something like:

        egen ok=group(id_exam course_year degree_course)
        keep if (ok==i & exam_year==t) & (ok==i & exam_year==t+1)
        I agree with Eric: Some sample data would make it easier for other members to help you. Meanwhile, notice that exam_year cannot be equal to both t and t+1 on the same row. So I think you want an OR where you have an AND. And in that case, you would rewrite your keep if line as follows (I think):

        Code:
        keep if (ok==i) & ((exam_year==t) | (exam_year==t+1))
        --
        Bruce Weaver
        Email: [email protected]
        Version: Stata/MP 19.5 (Windows)

        Comment

        Working...
        X