Announcement

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

  • Selecting and flagging unique minimum values by groups

    Hi everyone,

    I am having a problem with flagging an index variable. I want to flag the lowest value for a given variable, nrd_daystoevent, within a group, nrd_visitlink.

    My sample codes are below:
    *I installed ssc inst unique to use the egen nvals ( ) function

    egen uniq_daystoevent = nvals(nrd_daystoevent), by(nrd_visitlink)

    My output is below:

    nrd_visitlink nrd_daystoevent uniq_daystoevent
    000029 17609 1
    00008o 15782 2
    00008o 15828 2
    00009m 14265 2
    00009m 14287 2
    0000et 15664 1
    0000tb 19796 3
    0000tb 19825 3
    0000tb 19889 3


    What I want is for there to be only ONE flag (in the uniq_daystoevent variable) and not multiple flags. So in the output above, I would want nrd_visitlink 00008o to have a flag for the value '15782' since this value is lower than '15828'

    Thank you in advance for the help!

  • #2
    Some confusion here.

    The egen function nvals() is nothing to do with unique (SSC). It is part of egenmore (SSC).

    More crucially, it counts the number of distinct values, so is nothing at all to do with which is a minimum. Nor is a minimum ipso facto unique, meaning a value that occurs just once.

    The smallest value is flagged directly by

    Code:
    bysort nrd_visitlink (nrd_daystoevent): gen is_minimum = nrd_daystoevent == nrd_daystoevent[1]
    and if there are ties in any group then two or more observations in that group will receive the same flag of 1. You may not be worried about ties, but being smallest does not guarantee uniqueness in general.

    See also this FAQ: http://www.stata.com/support/faqs/da...ate/index.html

    Comment


    • #3
      wow, that is amazing! the code works beautifully! Thanks, Nick!
      Was wondering if you provide me with the syntax for your code or a link to that code? am I to assume that when to flag a minimum value within a given variable you create this statement

      code: bysort [variable-to-sort] (variable-name-to-flag): gen newvar = variable-name-to-flag = variable-name-to-flag[(the number 1 is the minimum value)]

      What about to flag the maximum value?

      thank you again for your insight!

      Comment


      • #4
        Getting the maximum is already discussed in the link already given!

        -- which also contains a reference discussing the code.

        Comment


        • #5
          Ah, I see it in the FAQ. Thank you!

          Comment

          Working...
          X