Announcement

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

  • Inspecting row variables for desired increasing trend (and detecting non-desired level or decreasing trend)

    In my dataset, a calculated field for age is being used to track subjects' progress. Code for a simulation is provided below.
    Rather than manually inspect >1200 records, I wish to detect whether age (when observations are sorted on id & date)
    increases (as in id==1), or does not change (as in id==2), or decreases (as in id==3). Is there a way to do this?

    Code:
    set more off
    clear
    set obs 9
    input id age str9 date
        1    40.0    21jan2016
        1    40.5    15jun2016
        1    40.9 10dec2016
        2    40.0    21jan2016
        2    40.0    15jun2016
        2    40.0    21jan2016
        3    40.9 21jan2016 
        3    40.5    15jun2016
        3    40.0    10dec2016 end
    gen sif_date = date(date, "DMY")
    format sif_date %td
    drop date
    sort id sif_date
    l, noo sepby(id)

  • #2
    I'm not sure how an age would decrease/stay the same (as the date increases), so I just want to check. Do you mean decreases/increases/stays the same across all dates or just date to date?

    Comment


    • #3
      I presume you're trying to catch possible errors, so what about the following?
      Code:
      egen any_increase = total((age > age[_n-1])& (_n > 1) ), by(id)
      egen any_decrease = total((age < age[_n-1]) & (_n > 1)), by(id)
      by id: gen all_equal = (any_increase == 0) & (any_decrease == 0)

      Comment


      • #4
        Thanks Mike for the elegant suggestion, which did flag some errors. Much appreciated!
        And apologies to Andrew for my somewhat ambiguous question.

        Comment

        Working...
        X