Statalist


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

st: RE: AW: count with a loop


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: AW: count with a loop
Date   Wed, 16 Dec 2009 12:58:42 -0000

Background reading: 

SJ-7-4  dm0033  . . . . . . Speaking Stata: Counting groups, especially panels
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q4/07   SJ 7(4):571--581                                 (no commands)
        discusses how to count panels through reduction commands
        or through tabulation commands and how to overcome
        problems that do not yield easily to these approaches

SJ-7-1  pr0029  . . . . . . . . . . . . . . .  Speaking Stata: Making it count
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q1/07   SJ 7(1):117--130                                 (no commands)
        discusses count used with a loop over observations
        or variables

Nick 
[email protected] 

Martin Weiss

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! 


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