Announcement

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

  • Using min and max values for calculations

    sortorder name cum_pct delta
    1 Bob 0.2 0.4
    2 Alice 0.5 0.1
    3 Eugene 0.58 0.02
    4 Theresa 0.7 0.1
    5 Thomas 1 0.3




    Hello,

    I have a cumulative percentage variable like the one below, and I want to figure out which observation is the closest to an arbitrary threshold, say 60%. I used the following line:

    Code:
    bysort sortorder: gen delta = min(abs(cum_pct - 0.6), cum_pct)
    I was able to get the absolute value distance between each observation and my threshold. However, I can not figure out how to return the name/sort order number associated with the smallest delta, so I can use it as a parameter in another function I am using. Is there a way do this in stata?

    Thanks!

  • #2
    You could do
    Code:
    sort delta
    local min_sortorder = sortorder[1]
    and let's just hope you don't have and/or don't care about ties in the minimum delta value.

    Comment

    Working...
    X