Announcement

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

  • Identifying specific observation in panel data

    Hi,

    I have a dataset where I have multiple observations per individual. I am looking for a specific observation, per each individual, among the many.

    Specifically, I am looking for the observation in which the value of a variable is closest to 1.

    How would I get about doing so? I have provided sample data in this post.

    Much appreciated,
    Kevin


    id value indicator
    1 0.85 Not this one
    1 0.99 This one!
    1 1.20 Not this one
    2 0.87 Not this one
    2 0.88 Not this one
    2 1.01 This one!

  • #2
    I just realized how to solve this.

    Subtract each value by 1 and gen newvar to take the absolute value. Then sort newvar for minimum.

    If you have a more elegant solution, I would love to hear it.

    Thanks.

    Comment


    • #3
      If setting a cut-off around +1/-1 makes sense, what follows is a proposal for doing the trick:

      Code:
      keep if value>=.99 & value<=1.01
      Kind regards,
      Carlo
      Kind regards,
      Carlo
      (Stata 18.0 SE)

      Comment


      • #4
        Kevin:
        my previous reply assumed that you want to delete the observations that do not fulfill your desired requirement.
        If this is not the case, you may want to try what follows:

        Code:
        g obs_OK=1 if value>=.99 & value<=1.01
        replace obs_OK =0 if obs_OK ==.
        list id value indicator if obs_OK==1
        Kind regards,
        Carlo
        Kind regards,
        Carlo
        (Stata 18.0 SE)

        Comment

        Working...
        X