Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Looping Dates & IDs


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Looping Dates & IDs
Date   Thu, 24 Nov 2005 14:08:26 -0000

Without knowing any more than Svend does what 
is in Raphael's -hem.do-, it does seem likely
that the whole strategy of producing hundreds 
of little data files and then -append-ing them 
could be replaced by using just one file and 
looping over the possibilities in place. 

Nick 
[email protected] 

Svend Juul
 
> Raphael wrote:
> 
> I have a do-file called hem.do which I would like to run. But before I
> run this do-file I need to drop some observations if dov >= d(1jan90).
> Hence
> 
> use data, clear
> drop if dov >= d(1jan90)
> do hem.do
> keep if id==5
> save newdata1, replace
> 
> I would like to repeat this process about a thousand times with
> different dates and ids located in a separate file (SEE SNIPPET) then
> post to a single file. For example,
> 
> use data, clear
> drop if dov >= d(2feb90)
> do hem.do
> keep if id==5
> save newdata2, replace
> 
> use newdata1, clear
> forvalues i=2/1000 {
>       append using newdata`i'
> }
> 
> SNIPPET
> id   date_of_onset
> 5    1jan90
> 5    2feb90
> 5    6jun96
> 7    10oct97
> 7    25dec99
> 
> Can this be done?
> --------------------------------------------------------------
> ----------
> --------
> 
> Raphael,
> 
> Take a look at this; it may - or may not - do what you want.
> 
> Good luck,
> Svend
> 
> // generating testdata
> clear
> input id str7 sdov
> 1 1jan89
> 2 2jan96
> 5 2jan90
> 7 11oct97
> end
> gen dov=date(sdov,"dmy",2006)
> format dov %d
> drop sdov
> save data , replace
> 
> // generating snippet
> clear
> input nid str7 sonset
> 5  1jan90
> 5  2feb90
> 5  6jun96
> 7  10oct97
> 7  25dec99
> end
> gen onset = date(sonset,"dmy",2006)
> format onset %d
> drop sonset
> local N=_N
> save snippet , replace
> 
> // combine them, snippet first; it has `N' observations
> append using data
> save data2 , replace
> 
> . list
> 
>       +----------------------------------+
>       | nid       onset   id         dov |
>       |----------------------------------|
>    1. |   5   01jan1990    .           . |
>    2. |   5   02feb1990    .           . |
>    3. |   5   06jun1996    .           . |
>    4. |   7   10oct1997    .           . |
>    5. |   7   25dec1999    .           . |
>       |----------------------------------|
>    6. |   .           .    1   01jan1989 |
>    7. |   .           .    2   02jan1996 |
>    8. |   .           .    5   02jan1990 |
>    9. |   .           .    7   11oct1997 |
>       +----------------------------------+
> 
> forvalues i=1/`N' {
>    use data2 , clear
>    drop if dov >= onset[`i'] & dov < .
>    do hem.do
>    keep if id==nid[`i']
>    save newdata`i' , replace
> }
> 
> use newdata1 , clear
> forvalues i=2/`N' {
>    append using newdata`i'
> }
> 
> . list
> 
>       +------------------------------+
>       | nid   onset   id         dov |
>       |------------------------------|
>    1. |   .       .    5   02jan1990 |
>    2. |   .       .    5   02jan1990 |
>    3. |   .       .    7   11oct1997 |
>       +------------------------------+

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