Statalist


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

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


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   AW: st: AW: finding date occurrences within a specified range, searching in multiple variables
Date   Fri, 9 Oct 2009 22:04:46 +0200

<> 

Well, my code has -ds- assemble all the variables beginning with "t_" and
feeds them to the -foreach- loop. Just insert a line such as - di in r
"`var'"- in the example below to see that:


*************
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)'{
	di in r "`var'"
	gen byte seek`var' = `var' > td(1nov2009) & t_date < td(3nov2009)
}

list, noobs
*************

You could also specify 

- foreach var of varlist t_*{ - 

and be just as happy :-), but -ds- is a more general approach...


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Michael
McCulloch
Gesendet: Freitag, 9. Oktober 2009 18:17
An: statalist@hsphsun2.harvard.edu
Betreff: Re: st: AW: finding date occurrences within a specified range,
searching in multiple variables 

Thanks, I'll study that.
In the -foreach- call below, is there a way to broadly specify all  
variables beginning with t_, rather than just that single variable  
t_date?


On Oct 9, 2009, at 9:05 AM, Martin Weiss wrote:

>
> <>
>
> 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
>
> *************
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Michael
> McCulloch
> Gesendet: Freitag, 9. Oktober 2009 18:00
> An: Statalist Statalist
> Betreff: st: finding date occurrences within a specified range,  
> searching in
> multiple variables
>
> 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))
>
>
>
>
> Michael McCulloch, LAc MPH PhD
> Pine Street Foundation
> 124 Pine Street
> San Anselmo, CA 94960-2674
> tel:	415-407-1357
> fax: 	206-338-2391
> mm@pinestreetfoundation.org
>
>
>
>
>
> *
> *   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/



Michael McCulloch, LAc MPH PhD
Pine Street Foundation
124 Pine Street
San Anselmo, CA 94960-2674
tel:	415-407-1357
fax: 	206-338-2391
mm@pinestreetfoundation.org








*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index