Chris
How about this (it requires Nick Cox and Richard Goldstein's -spell- , use -findit spell- to locate and download):
cl, noobs
gen time = _n
tsset time
gen diff_pressure = d.pressure
gen sign = cond(diff>=0, 1 , -1)
spell sign
bysort _spell: gen cumul = sum(diff)
qui sum cumul
gen max_drop = cond(r(min)< 0, r(min), 0)
l, abb(16) noobs
Which produces the following:
. cl, noobs
pressure
90
100
90
80
70
80
65
70
90
80
70
. gen time = _n
. tsset time
time variable: time, 1 to 11
. gen diff_pressure = d.pressure
(1 missing value generated)
. gen sign = cond(diff>=0, 1 , -1)
. spell sign
. bysort _spell: gen cumul = sum(diff)
. qui sum cumul
. gen max_drop = cond(r(min)< 0, r(min), 0)
. l, abb(16) noobs
+----------------------------------------------------------------------------------+
| pressure time diff_pressure sign _spell _seq _end cumul max_drop |
|----------------------------------------------------------------------------------|
| 90 1 . 1 1 1 0 0 -30 |
| 100 2 10 1 1 2 1 10 -30 |
| 90 3 -10 -1 2 1 0 -10 -30 |
| 80 4 -10 -1 2 2 0 -20 -30 |
| 70 5 -10 -1 2 3 1 -30 -30 |
|----------------------------------------------------------------------------------|
| 80 6 10 1 3 1 1 10 -30 |
| 65 7 -15 -1 4 1 1 -15 -30 |
| 70 8 5 1 5 1 0 5 -30 |
| 90 9 20 1 5 2 1 25 -30 |
| 80 10 -10 -1 6 1 0 -10 -30 |
|----------------------------------------------------------------------------------|
| 70 11 -10 -1 6 2 1 -20 -30 |
+----------------------------------------------------------------------------------+
Hope this helps,
Scott
----- Original Message -----
From: chris wallace <[email protected]>
Date: Tuesday, March 2, 2004 6:59 am
Subject: st: help to find maximum drop in a variable
> My colleague has passed to me a query to which I can't find a
> straightforward answer. I am hoping some of the experts in this group
> could help?
>
> She has hourly barometric pressure data for many days. So the dataset
> contains the variables
>
> year month day hour pressure
>
> (with 24 hourly observations per day). During any day the
> pressure may
> rise and fall. She wishes to generate a new variable containing the
> maximum fall (constant within year-month-day groups). That is,
> for a
> fictitious series of pressure readings
>
> 90 100 90 80 70 80 65 70 90 80 70
>
> the maximum fall is 100-70=30. This is not simply the maximum -
> minimum(100-65), since the pressure rises for one interval in that
> period(70->80).
>
> She also makes the points:
> - there may be more than one drop per day... i want the largest one
> - if pressure has not dropped at all through out the day, the value
> returned should be zero or negative
>
> Any suggestions?
>
> Many thanks, Chris.
>
*
* 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/