Announcement

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

  • Last occurrence of an id

    Hello everyone,

    I would like to know what is the equivalent of last. command of SAS for stata, if the last occurrence is not a value 0 or 1.

    For example I have a dataset with id and years:

    id year

    1 2002
    2 2003
    3 2004
    4 2005

    I would like to generate a variable called "LAST" and taking the value of 1 if the row is the last occurrence of the variable and 0 otherwise (equivalent of SAS: if last.id then last=1).

    The solution given in the FAQ applies to a stata coded 1 or 0. I could not find the answer in STATA for a variable that changes but without typing the value of the variable (like done with state in the FAQ)

    I thank you in advance for your help !!!

    Kindly,

    Eugénie

  • #2
    Which FAQ? There are hundreds of them! Also, Stata please (not STATA or state).


    Code:
    bysort id (year) : gen is_last = _n == _N
    may be what you seek.

    Comment


    • #3
      Code:
      by id (year), sort: gen byte last = (_n == _N)
      I urge you to take some time to read the Getting Started [GS] and User's Guide [U] volumes of the PDF documentation that comes with your Stata installation. There you will learn about the overall Stata approach to data analysis and management as well as the fundamental commands and concepts that are used over and over again. It is a fairly long read, and you will not remember everything. But you will become sufficiently familiar with Stata that in most situations you will recognize which commands are likely to be useful in most situations, and then you can refer back to the help files or to the PDF documentation for the details. The time you spend doing this will be amply repaid.

      I think this is especially important for you as a former SAS user. Your habits formed in SAS will, in general, not be helpful in Stata. In fact, your SAS instincts will sometimes lead you astray in Stata. So I think trying to immerse yourself in Stata's way of thinking is even more important than it is for somebody with no prior statistical package experience--you have old habits to unlearn.

      Edit: Crossed with #2, which proposes the same solution.

      Comment


      • #4
        http://www.stata-journal.com/sjpdf.h...iclenum=pr0004 may help on by:

        Comment


        • #5
          Thank you very much to both of you. Indeed, I though that my large experience in SAS would have helped me, but STATA has a very different way of functioning. I will look at this documentation.

          Eugénie

          Comment

          Working...
          X