Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: min and max with dates


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: min and max with dates
Date   Mon, 8 Oct 2007 19:15:07 +0100

by CODE, sort: egen m = min(real(newdate1)) 

would seem to be an alternative. But if you 
wanted to use a numeric version of -newdate1-
later, using -destring- would be better. 

In addition, note that there is an FAQ on
this issue: 

How can I generate a variable containing the last of 
several dates? 
http://www.stata.com/support/faqs/data/lastdate.html

which despite its title does cover minimum as 
well as maximum. 

Nick 
[email protected] 

Sergiy Radyakin
 
> Sorry, Gaby, I was not particularly careful with the syntax, just
> wanted to communicate the idea. To be more precise, the resulting
> variable is a string, so "min" does not work with it. So, first we
> need to:
> 
> destring newdate1, generate(nd1)
> 
> Now nd1 is a numeric variable. And second, don't write the parenthesis
> () after by:
> 
> by CODE: egen m=min(nd1)
> 
> 
> . l
> 
>      +----------------+
>      | CODE   newdate |
>      |----------------|
>   1. |   10      0410 |
>   2. |   10      0414 |
>   3. |   10      0408 |
>   4. |   10      0301 |
>   5. |   20      0208 |
>      |----------------|
>   6. |   20      0216 |
>   7. |   20      0208 |
>   8. |   20      0210 |
>      +----------------+
> 
>      +----------------------------+
>      | CODE   newdate    nd1  beg |
>      |----------------------------|
>   1. |   10      0301   301   301 |
>   2. |   10      0408   408   301 |
>   3. |   10      0410   410   301 |
>   4. |   10      0414   414   301 |
>   5. |   20      0208   208   208 |
>      |----------------------------|
>   6. |   20      0208   208   208 |
>   7. |   20      0210   210   208 |
>   8. |   20      0216   216   208 |
>      +----------------------------+
 
Ana Gabriela Guerrero Serdan 

> > Thanks, I managed to swap the month first and then the
> > day. But when trying to calculate the minimum it says
> >
> > by(CODE): egen beg=min(newdate1)
> > invalid syntax
> > r(198);
> >
> > I been trying to calculate the mimimum of newdate
> > without the sort and its also not possible... !?
> > Should I firs set the var as date?

Sergiy Radyakin

> > > this will swap the day and month in the [string]
> > > variable Date1:
> > > gen newdate1=substr(Date1,3,2)+substr(Date1,1,2)
> > > As long as Date2 is always later than Date1, you
> > > care only about Date1
> > > for the minimum:
> > > sort CODE newdate1
> > > by(CODE): egen beg=min(newdate1)
> > >
> > > Quite similarly you proceed to the end dates.

Ana Gabriela Guerrero

 > > > I have a very basic question but I havent find the
> > > > date commands when I only have day and month.
> > > >
> > > > I have information on interviews carried out and
> > > want
> > > > to know the range of interview per province
> > > (CODE), so
> > > > I want to calculate the min and max of the dates
> > > > within each province.
> > > >
> > > >
> > > > . list  CODE Date1 Date2 Date3
> > > >
> > > >       +-------------------------------+
> > > >       |    CODE   Date1 Date2 Date3   |
> > > >       |-------------------------------|
> > > >    1. |     01     2704               |
> > > >    2. |     01     2704   3004        |
> > > >    3. |     01     2704         1505  |
> > > >    4. |     01     2505               |
> > > >    5. |     01     2605               |
> > > >       |-------------------------------|
> > > >    6. |     02     2705   0106        |
> > > >    7. |     02     2805    0606       |
> > > >
> > > > etc..

*
*   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/



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