Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: AW: RE: RE: Identify min and max observations


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: RE: RE: Identify min and max observations
Date   Mon, 28 Sep 2009 13:07:06 +0200

<> 

" Once the minima and maxima have been determined, flagging
which observations equal them does not require the aegis of -by:-."


Even though no harm is done by these prefixes, I am all for shortening code
and being as concise as possible. For pedagogical reasons, I should indeed
have omitted them. But it was already one o` clock on our election night, so
please forgive me...

Also for pedagogical reasons, let`s explore the shorter answer to Michael`s
question that I alluded to in my earlier post:

***
clear*

inp id str10  admdate      
1  12may06       
1  20jun06
1  30aug06       
2  10may06       
2  10sep06
2  20dec06
2  10jan07       
3  10aug06       
3  20sep06
3  10dec06
3  15jan07
3  10feb07  
end

compress

gen date=date(admdate, "DM20Y")

bys id: gen byte mindate=_n==1
by id:  gen byte maxdate=_n==_N

list, noobs sepby(id)
***



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Nick Cox
Gesendet: Montag, 28. September 2009 12:24
An: [email protected]
Betreff: st: RE: RE: Identify min and max observations

Some nuances in understanding this code:

In the first pair of statements below, the -bys id- is essential as the
whole point is that minimum and maximum are to be determined within
-id-.  

bys id: egen `min'=min(date)
bys id: egen `max'=max(date)

(In fact, the second -bys- could be just -by:-, because at that point we
know the data are sorted as desired.) 

In the second pair of statements below, the -bys id- does no harm but is
dispensable. Once the minima and maxima have been determined, flagging
which observations equal them does not require the aegis of -by:-. 

bys id: gen byte mindate=date==`min'
bys id: gen byte maxdate=date==`max'

They could therefore be 

gen byte mindate=date==`min'
gen byte maxdate=date==`max'

Nick 
[email protected] 

Martin Weiss

You want your admdate to be numeric for sure. You could then -sort-
within
id and take the first and last value. A more general solution is:

**************
clear*

inp id str10  admdate      
1  12may06       
1  20jun06
1  30aug06       
2  10may06       
2  10sep06
2  20dec06
2  10jan07       
3  10aug06       
3  20sep06
3  10dec06
3  15jan07
3  10feb07  
end

compress

gen date=date(admdate, "DM20Y")
format date %tdMonth_DD,_CCYY

list, noobs  

tempvar min max
bys id: egen `min'=min(date)
bys id: egen `max'=max(date)

bys id: gen byte mindate=date==`min'
bys id: gen byte maxdate=date==`max'

list, noobs sepby(id)

Thomas Cars

I have a dataset including subjects (id) and date for administration  
of a specific drug (adm.date)

I now want to construct a variable telling me which one of the  
observations (adm.date) in each subject (id) that has the lowest date  
(min) and the highest date(max).

The number of adm.date can vary in each subject from 1 to 10.


Example (min=1 och max=2)



Id                   adm.date        min/max

1                    12may06         1
1                    20jun06
1                    30aug06         2
2                    10may06         1
2                    10sep06
2                    20dec06
2                    10jan07         2
3                    10aug06         1
3                    20sep06
3                    10dec06
3                    15jan07
3                    10feb07         2

And so on?


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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