Announcement

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

  • Anywatch

    Hi, my data is sorted by caseid, year and episodes. I want to find the first non zero value in variable event by caseid, year. If the data was in wide format anymatch command could have solved the purpose but since it is in long form. I am not sure how to solve it. Also I am not very acquainted of looping in stata. If anybody have any ideas about it please share. Thanks!! The format of my data is given below
    caseid year episode event
    1100122 1 1 0
    1100122 1 2 B
    1100122 2 1 B
    1100122 2 2 0
    1100122 2 3 B

  • #2
    I'm not quite sure of what it is you're after, but maybe something like the following will give you some ideas to pursue.

    .ÿversionÿ15.1

    .ÿ
    .ÿclearÿ*

    .ÿinputÿstr7ÿcaseidÿbyteÿ(yearÿepisode)ÿstr1ÿevent

    ÿÿÿÿÿÿÿÿcaseidÿÿÿÿÿÿyearÿÿÿepisodeÿÿÿÿÿÿevent
    ÿÿ1.ÿ1100122ÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿ0
    ÿÿ2.ÿ1100122ÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿB
    ÿÿ3.ÿ1100122ÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿB
    ÿÿ4.ÿ1100122ÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿ0
    ÿÿ5.ÿ1100122ÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿ3ÿÿÿÿÿÿÿB
    ÿÿ6.ÿend

    .ÿ
    .ÿtempfileÿdataset

    .ÿquietlyÿsaveÿ`dataset'

    .ÿ
    .ÿ//ÿSetÿup
    .ÿquietlyÿkeepÿifÿeventÿ!=ÿ"0"

    .ÿquietlyÿbysortÿcaseidÿyearÿ(episode):ÿkeepÿifÿ_nÿ==ÿ1

    .ÿrenameÿeventÿevent0

    .ÿgenerateÿstrÿtagÿ=ÿ"<="

    .ÿ
    .ÿtempfileÿmarked

    .ÿquietlyÿsaveÿ`marked'

    .ÿ
    .ÿ//ÿIfÿyouÿjustÿwantÿtoÿtagÿtheÿfirstÿnonzeroÿeventÿperÿcaseidÿandÿyear
    .ÿmergeÿ1:1ÿcaseidÿyearÿepisodeÿusingÿ`dataset',ÿassert(matchÿusing)ÿnogenerateÿnoreport

    .ÿ
    .ÿsortÿcaseidÿyearÿepisode

    .ÿlistÿcaseidÿyearÿepisodeÿeventÿtag,ÿnoobsÿsepby(caseidÿyear)ÿabbreviate(20)

    ÿÿ+----------------------------------------+
    ÿÿ|ÿÿcaseidÿÿÿyearÿÿÿepisodeÿÿÿeventÿÿÿtagÿ|
    ÿÿ|----------------------------------------|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿ|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿBÿÿÿÿ<=ÿ|
    ÿÿ|----------------------------------------|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿBÿÿÿÿ<=ÿ|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿ|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿÿ3ÿÿÿÿÿÿÿBÿÿÿÿÿÿÿ|
    ÿÿ+----------------------------------------+

    .ÿ
    .ÿ//ÿIfÿyouÿwantÿtoÿprovideÿtheÿfirstÿnonzeroÿeventÿvalueÿ.ÿ.ÿ.
    .ÿuseÿ`marked',ÿclear

    .ÿdropÿepisode

    .ÿmergeÿ1:mÿcaseidÿyearÿusingÿ`dataset',ÿassert(matchÿusing)ÿnogenerateÿnoreport

    .ÿ
    .ÿsortÿcaseidÿyearÿepisode

    .ÿlistÿcaseidÿyearÿepisodeÿeventÿevent0,ÿnoobsÿsepby(caseidÿyear)ÿabbreviate(20)

    ÿÿ+-------------------------------------------+
    ÿÿ|ÿÿcaseidÿÿÿyearÿÿÿepisodeÿÿÿeventÿÿÿevent0ÿ|
    ÿÿ|-------------------------------------------|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿÿBÿ|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ1ÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿBÿÿÿÿÿÿÿÿBÿ|
    ÿÿ|-------------------------------------------|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿÿ1ÿÿÿÿÿÿÿBÿÿÿÿÿÿÿÿBÿ|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿÿ2ÿÿÿÿÿÿÿ0ÿÿÿÿÿÿÿÿBÿ|
    ÿÿ|ÿ1100122ÿÿÿÿÿÿ2ÿÿÿÿÿÿÿÿÿ3ÿÿÿÿÿÿÿBÿÿÿÿÿÿÿÿBÿ|
    ÿÿ+-------------------------------------------+

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .


    I don't know why you think you need to loop.

    Comment


    • #3
      Please use dataex as requested in the FAQ Advice.

      Also, please follow the request to explain where community-contributed programs you refer to come from. anymatch [not "anywatch"] is a program by Roy Wada on SSC. I've never used it, so can't comment.

      More importantly, I can't work out what you want with confidence, but this may help.

      The first date or time (subject to conditions) is just the minimum date or time (subject to those conditions).

      More at Stata FAQ https://www.stata.com/support/faqs/d...t-occurrences/

      Code:
      clear 
      input caseid    year    episode    str1 event
      1100122    1    1    0
      1100122    1    2    B
      1100122    2    1    B
      1100122    2    2    0
      1100122    2    3    B
      end 
      
      sort caseid year episode 
      by caseid: gen counter = _n 
      by caseid year: egen when_first = min(cond(event != "0", counter, .)) 
      gen was_first = counter == when_first 
      
      list, sepby(caseid year) 
      
           +------------------------------------------------------------------+
           |  caseid   year   episode   event   counter   when_f~t   was_fi~t |
           |------------------------------------------------------------------|
        1. | 1100122      1         1       0         1          2          0 |
        2. | 1100122      1         2       B         2          2          1 |
           |------------------------------------------------------------------|
        3. | 1100122      2         1       B         3          3          1 |
        4. | 1100122      2         2       0         4          3          0 |
        5. | 1100122      2         3       B         5          3          0 |
           +------------------------------------------------------------------+

      Comment


      • #4
        Thanks Joseph. Thanks for the several hints. It was very helpful.

        Comment

        Working...
        X