Statalist


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

AW: st: RE: AW: finding date occurrences within a specified range, searching in multiple variables


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: RE: AW: finding date occurrences within a specified range, searching in multiple variables
Date   Fri, 9 Oct 2009 22:34:17 +0200

<> 

Here is an example where any observation is -list-ed where any of the dates
is within the range 11/1/2009 to 11/9/2009:


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

inp str10 date
"11012009"
"11012009"
"11022009"
"11032009"
"11012009"
"10082009"
end

gen byte id = _n


gen t_date = date(date, "MDY")
drop date
generate t_anotherdate=t_date+7
gen t_yetanotherdate=t_date+8
format t_* %td


foreach var of varlist t_*{
	gen byte seek`var' =  /* 
	*/ inrange(`var', td(1nov2009), /* 
	*/ td(9nov2009))
}

egen rmax=rowmax(seek*)

di in r "Condition is true: "
li if rmax, noo

di in r  _n "Condition is false: "
li if !rmax, noo
*************



HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected]
[mailto:[email protected]] Im Auftrag von Michael
McCulloch
Gesendet: Freitag, 9. Oktober 2009 19:04
An: [email protected]
Betreff: Re: st: RE: AW: finding date occurrences within a specified range,
searching in multiple variables

Thanks Nick, what I meant more broadly was whether multiple date  
variables, all whose names begin with "t_", for example t_date1,  
t_date2, etc., could be specified in this line, in which the second  
date is changed for a wider time frame:

> foreach var of varlist `r(varlist)'{
> 	gen byte seek`var' = `var' > td(1nov2009) & t_date < td(9nov2009)


I also attempted a modification, which rather than giving me the id of  
only records in which any date (t_*) was within 1nov to 9nov, instead  
gave me all records and all dates:

qui ds t_*
sort id
foreach var of varlist `r(varlist)'{
	gen byte seek`var' = `var' > td(1nov2009) & `var' < td(9nov2009)
	list id seek* if `var'!=0
}




On Oct 9, 2009, at 9:37 AM, Nick Cox wrote:

> If these are daily data, being later than 1 nov 2009 and earlier  
> than 3
> nov 2009 (which the code clearly implies) reduces to being equal to 2
> nov 2009, so you just need a single equality.
>
> In addition, check out -tin()- and -twithin()- which can lead to  
> simpler
> code.
>
> Nick
> [email protected]
>
> Martin Weiss
>
> You may also find -help time_series_functions- useful...
>
> *************
> clear
> set obs 5
> gen id = _n
> gen date="11012009"
> replace date="11022009" in 3
> replace date="11032009" in 4
> list date
> generate t_date = date(date, "MDY")
> generate t_anotherdate=t_date+2
> format t_* %td
>
> qui ds t_*
>
> foreach var of varlist `r(varlist)'{
> 	gen byte seek`var' = `var' > td(1nov2009) & t_date <
> td(3nov2009)
> }
>
> list, noobs
>
> *************
>
> Michael McCulloch
>
> I'm looking for events that all occurred within a specified date
> range, searching in multiple date fields. I've created the following
> example to show that I want to find all records where t_date is
> between 1nov2009 and 3nov2009.
>
> This works fine, but is there a way to create a loop that would do
> this search within multiple date fields, all of which have field name
> beginning with "t_"?
>
> clear
> set obs 5
> gen id = _n
> gen date="11012009"
> replace date="11022009" in 3
> replace date="11032009" in 4
> list date
> generate t_date = date(date, "MDY")
> format t_date %td
> gen seek = cond((t_date > td(1nov2009) & t_date < td(3nov2009)), 1, 0)
> sort seek
> list id t_date seek if seek==1
> list id t_date if (t_date > d(1nov2009) & t_date < d(3nov2009))
>
>
> *
> *   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/



Michael McCulloch, LAc MPH PhD
Pine Street Foundation
124 Pine Street
San Anselmo, CA 94960-2674
tel:	415-407-1357
fax: 	206-338-2391
[email protected]






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