Announcement

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

  • Maximum dates

    Hi all,

    If I have panel data and I want to create a new variable containing the maximum of another variable how would I do that?

    The obvious answer would be bysort var: egen newvar=max(oldvar).

    But, what if I want the value to appear only once for each id AND I want to replace the max of oldvar with the earliest date for ids that had an event?

    Example:

    id var1 var2 newvar
    1 3 0
    1 4 0
    1 7 0 7
    2 13 1 13
    2 50 0
    2 100 1
    3 15 0
    3 20 1 20
    3 23 1




  • #2
    This is a little cryptic -- you mention maximum dates but also mention first occurrences -- but the first occurrence of var2 == 1 gives

    Code:
    egen first1 = min(var1 / (var2 == 1)) , by(id)
    on which see e.g. http://www.stata-journal.com/sjpdf.h...iclenum=dm0055

    Some might prefer

    Code:
    egen first1 = min(cond(var2 == 1, var1, .)) , by(id)
    See also http://www.stata.com/support/faqs/da...t-occurrences/


    Comment


    • #3
      Basically the maximum date will apply to those who did not have an event and for those who did I want the date to be the one that applied to the first occurence.

      Comment


      • #4
        This has a lot more steps but it works:

        Code:
        clear
        
        input id var1 var2 newvar
        1 3 0 .
        1 4 0 .
        1 7 0 7
        2 13 1 13
        2 50 0 .
        2 100 1 .
        3 15 0 .
        3 20 1 20
        3 23 1 .
        end
        
        egen max=max(var1), by(id)
        egen min=min(var1) if var2==1, by(id)
        
        *if event>1, the id has had an event
        egen event=sum(var2), by(id)
        
        gen newvar2=.
        
        *if there is no event for id, use the max
        replace newvar2=max if event==0 & max==var1
        
        *if there is an event, use the min
        bysort id: replace newvar2=min if event>0 & min==var1
        
        list
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          The two step completion of my answer seems like

          Code:
          egen first1 = min(cond(var2 == 1, var1, .)) , by(id)
          bysort id (var1) : replace first1 = var1[_N] if missing(first1)

          Comment

          Working...
          X