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