Announcement

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

  • finding maximum value that satisfies conditions

    I have been trying to find a solution to this, but haven't found it (or haven't understood that it does what I am looking for when I have seen it), so I am posting here. Apologies if this question is a bit rudimentary!

    I have a variable called sequence that takes on integer values 0 to 7. I want to be able to generate some summative information about this by student and term. For example, I can find the maximum value of sequence for each student per term by writing:

    bysort StudentID Term : egen maxenrollseq=max( sequence )

    This works as intended. But now I want to generate the maximum value of sequence (again by student by term) for which the value of another variable is 1. I just can't figure out how to do this?

    For example, considering the follow made up data (where blank cells are missing data):
    StudentID Term sequence var
    1 1 3 1
    1 1 4 0
    1 1 4
    1 2 4
    I would want student 1 to have a value of 3 for the maxseqvar value in term 1, and to have maxseqvar missing for term 2 (so that the new data would be):
    StudentID Term sequence var maxseqvar
    1 1 3 1 3
    1 1 4 0 3
    1 1 4 3
    1 2 4
    Thanks in advance for any help!
    Last edited by Claire Wladis; 16 Oct 2023, 12:40.

  • #2
    Code:
    bysort StudentID Term : egen maxenrollseq=max(cond(var == 1, sequence, .))

    Comment


    • #3
      For more on Clyde's approach, see Section 9 of https://journals.sagepub.com/doi/pdf...867X1101100210

      The rest of the paper might be interesting or useful.

      Comment


      • #4
        Thanks so much, Clyde Schechter and Nick Cox, this did exactly what I needed.

        Comment

        Working...
        X