Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: AW: count with forval and 2 conditions


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   st: AW: count with forval and 2 conditions
Date   Fri, 6 Aug 2010 09:28:46 +0200

<> 


So let me guess: You are referring to the thread at http://www.stata.com/statalist/archive/2009-12/msg00578.html ? The problem that I can see with your code is that you only -sort- on "timeposted". The -if- qualifier "region != region[`i']" also compares on the region even though you have not -sort-ed on it. I am not sure whether this is the issue spoiling your fun, but it could be. Maybe have a look at the judicious deployment of the -bysort- prefix, from NJC`s http://www.stata-journal.com/sjpdf.html?articlenum=pr0004




HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [email protected] [mailto:[email protected]] Im Auftrag von LY Pierrem
Gesendet: Freitag, 6. August 2010 09:12
An: [email protected]
Betreff: st: count with forval and 2 conditions

Dear Statalist,

I have data on funding applicants from different regions that look like this

id		timeposted		timefunded	comp	region
1	Feb 08, 2009 23:00:09	Feb 09, 2009 13:23:17	0	NAmerica
2	Feb 08, 2009 23:30:06	Feb 09, 2009 20:50:41	1	Asia
3	Feb 08, 2009 23:30:06	Feb 09, 2009 03:14:32	2	Asia
4	Feb 09, 2009 00:00:08	Feb 09, 2009 01:12:19	3	Asia
5	Feb 09, 2009 00:10:05	Feb 09, 2009 02:48:30	4	Asia
6	Feb 09, 2009 00:10:05	Feb 09, 2009 03:54:15	5	Asia
7	Feb 09, 2009 01:10:05	Feb 09, 2009 15:26:52	6	Asia
8	Feb 09, 2009 01:10:05	Feb 11, 2009 18:35:09	7	Asia
9	Feb 09, 2009 01:10:05	Feb 09, 2009 02:34:56	8	Africa
10	Feb 09, 2009 01:10:05	Feb 09, 2009 05:49:01	9	Asia
11	Feb 09, 2009 01:10:05	Feb 11, 2009 00:54:40	10	Asia
12	Feb 09, 2009 01:20:06	Feb 09, 2009 03:56:48	10	Africa
13	Feb 09, 2009 02:40:13	Feb 09, 2009 06:45:58	10	Africa
14	Feb 09, 2009 02:40:14	Feb 09, 2009 03:55:28	11	Africa
15	Feb 09, 2009 02:40:14	Feb 09, 2009 04:36:02	12	Africa
16	Feb 09, 2009 02:50:05	Feb 09, 2009 12:05:11	12	EastEurope
17	Feb 09, 2009 02:50:05	Feb 09, 2009 12:32:17	13	EastEurope
18	Feb 09, 2009 02:50:05	Feb 09, 2009 04:12:15	14	Africa
19	Feb 09, 2009 03:00:06	Feb 09, 2009 05:04:55	15	Africa
20	Feb 09, 2009 03:00:06	Feb 09, 2009 13:15:49	16	EastEurope
21	Feb 09, 2009 03:30:09	Feb 09, 2009 05:19:47	16	Africa


Timeposted is the time when a person was posted on the database of applicants. Timefunded is when she was funded. Thanks to earlier help from you, I successfully created a new variable, here labeled "comp" which for each person n, counts 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, "comp" is the number of people active (i.e. posted but not yet funded) on the database at the time each person n is posted.

Now, I am trying to add a condition, in order to count the people
who meet the first condition, timefunded<timeposted[`i'], (see below) but ALSO who are of the same region. For example, for person id #13, it should be 2, i.e. there are 2 other people from Africa who were active at the time #13 was posted (versus 10 other active people if one ignores the region condition, i.e. my "comp" variable above). 

The new count is called "comp2", using the code 

//maximum possible
sort timeposted
gen comp2=_n-1

//check second condition
//conditional on first being met
forv i =1/`=_N'{
    qui cou if timefunded<timeposted[`i'] & region != region[`i'] in 1/`i'
    replace comp2=comp2-`r(N)' in `i'
}


However, this does not do the right calculation.
(note: to get "comp", the code was exactly the same except without the region condition in the qui cou.

I would be immensely grateful for any suggestions!

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


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index