Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: var gen based on history


From   John Hennen <jhennen@mclean.org>
To   statalist@hsphsun2.harvard.edu
Subject   st: var gen based on history
Date   Sat, 12 Jul 2003 20:10:26 -0400

"Webb.Bayard" wrote:

> I have variables Category, Date and Amount and I need to generate two new
> variables described as follows.
>
> minAmount = smallest Amount in Category as of Date
> maxAmount = largest Amount in Category as of Date
>
> Example:
> Category                Date            Amount  min     max
> 1                       1/1/2001        50              50      50
> 2                       1/1/2001        100             100     100
> 1                       1/2/2001        52              50      52
> 2                       1/2/2001        98              98      100
> 1                       1/3/2001        47              47      52
> 2                       1/3/2001        105             98      105
> 1                       1/4/2001        55              47      55
> 2                       1/4/2001        93              93      105
>

REPLY:
This is sort of cludgy, but it seems to work:
 sort date category
 capture drop order
  gen byte order = _n
 capture drop min
 capture drop max
 sort category date
  foreach X of new min max {
    by category : gen `X' = amount[1]
    }
 qui by category : replace min = amount if amount < min[_n-1]
 qui by category : replace min = min[_n-1] if min > min[_n-1]
 qui by category : replace max = amount if amount > max[_n-1]
 qui by category : replace max = max[_n-1] if max < max[_n-1] & _n >1
 sort order
 list

john hennen
Boston




>
> I've looked, but I can't come up with the right gen or egen commands and
> I've not yet tackled programming. I'm hoping someone can spot some tricky
> way of accomplishing this.
>
> Any help would be appreciated.
>
> Bayard
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/



Any information, including protected health information (PHI), transmitted in this email is intended only for the person or entity to which it is addressed and may contain information that is privileged, confidential and or exempt from disclosure under applicable Federal or State law. Any review, retransmission, dissemination or other use of or taking of any action in reliance upon, protected health information (PHI) by persons or entities other than the intended recipient is prohibited. If you received this email in error, please contact the sender and delete the material from any computer.



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index