My apologies for the delayed follow up. Thank you for the suggested code and useful stata journal references.
The code seems to do what I want indeed, thanks! Now, I tried to add one more condition on the count, but it seems i am not putting it in the right place (cause it then counts things wrong). For example, counting the people who meet the already suggested timefunded<timeposted[`i'], but also who are, say, of the same region.
so I tried to expand one line under the forval:
qui cou if timefunded<timeposted[`i'] & region=region[`i'] in 1/`i'
but it ends up counting ALL the people of the same region, not just the people of the same region also meeting the first condition. Is there a place to add correctly this in the same code?
Thanks again, your code and reading suggestions are invaluable.
Pierre
--- En date de : Mer 16.12.09, Martin Weiss <[email protected]> a écrit :
> De: Martin Weiss <[email protected]>
> Objet: st: AW: count with a loop
> À: [email protected]
> Date: Mercredi 16 Décembre 2009, 9h34
>
> <>
>
> Try this very carefully to see whether it does what you
> want:
>
> *************
> clear*
>
> inp byte personid str20(dateposted datefunded)
> 1 "2009-04-04 3:20:08" "2009-04-04 10:00:14"
> 2 "2009-04-04 5:20:08" "2009-04-04 11:02:14"
> 3 "2009-05-04 2:20:08" "2009-05-04 11:02:14"
> 4 "2009-05-04 2:20:02" "2009-05-04 11:02:17"
> 5 "2009-05-04 2:12:02" "2009-05-04 11:04:17"
> end
>
> compress
>
>
> gen double timeposted= clock(dateposted, "YMDhms")
> gen double timefunded= clock(datefunded, "YMDhms")
>
> format timeposted timefunded /*
> */ %tcMon_DD,_CCYY_HH:MM:SS
>
> drop date*
>
> //maximum possible
> sort timeposted
> gen count=_n-1
>
> //check second condition
> //conditional on first being met
> forv i =1/`=_N'{
> qui cou if timefunded<timeposted[`i']
> in 1/`i'
> replace count=count-`r(N)' in `i'
> }
>
> li, noo
> *************
>
>
>
> HTH
> Martin
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected]
> [mailto:[email protected]]
> Im Auftrag von LY Pierrem
> Gesendet: Mittwoch, 16. Dezember 2009 09:16
> An: [email protected]
> Betreff: st: count with a loop
>
> Dear Stata list,
>
> I have a data set that looks like this
>
> personid
> dateposted datefunded
> 1 2009-04-04 3:20:08
> 2009-04-04 10:00:14
>
> 2
>
>
>
> I am trying to create a new variable, which for each person
> n, would
> count the number of people such that 1. they were posted
> before person
> n AND 2. were funded after person n was posted. In other
> words, this would
> count the number of people active (i.e. posted but not yet
> funded) on the
> database at the time each person n is posted.
>
> It seems that a simple egen and conditional count are not
> good enough for
> the purpose.
>
> Is a loop necessary for this? I have an idea that a loop is
> needed here
> (maybe using forval?) but any help would be greatly
> appreciated!
>
> Thanks for any suggestions!
> Pierre
>
>
>
>
> *
> * 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/
>
*
* 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/