Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Nick Cox <njcoxstata@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Average when -inrange()- |

Date |
Fri, 18 Nov 2011 08:48:17 +0000 |

If -price- is never missing, using the -count- command is redundant, as r(N) from -summarize- gives the same count. Nick On Fri, Nov 18, 2011 at 8:30 AM, Joseph Monte <hmjc66@gmail.com> wrote: > Thanks, Nick. I think I got it! > > sort group date2 > gen count1=. > gen mean1=. > qui forval i = 1/`=_N'{ > count if group==group[`i'] & > inrange(date2,ninetydaysbefore[`i'],date1[`i']) > replace count1 = r(N) in `i' > summarize price if group==group[`i'] & > inrange(date2,ninetydaysbefore[`i'],date1[`i']) > replace mean1 = r(mean) in `i' > } > > Joe > > > On Thu, Nov 17, 2011 at 11:33 PM, Nick Cox <njcoxstata@gmail.com> wrote: >> Just use the same -if- condition together with -summarize-. Same idea >> of initialising a variable before the loop and -replace- within the >> loop. >> >> Nick >> >> On Thu, Nov 17, 2011 at 10:06 PM, Joseph Monte <hmjc66@gmail.com> wrote: >>> Dear Statalisters, >>> >>> The code below gives me the number of observations (i.e. count1) when >>> "date2" occurs between two dates - "ninetydaysbefore" and "date1" - >>> based on "group". I also need the average of another variable called >>> "price" of the observations that are counted. I am not sure how to >>> incorporate it within the loop below. >>> >>> sort group date2 >>> gen count1=. >>> qui forval i = 1/`=_N'{ >>> count if group==group[`i'] & inrange(date2,ninetydaysbefore[`i'],date1[`i']) >>> replace count1 = r(N) in `i' >>> } >>> >>> I am using Stata 12. >>> * * 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/

**Follow-Ups**:**Re: st: Average when -inrange()-***From:*Joseph Monte <hmjc66@gmail.com>

**References**:**st: Average when -inrange()-***From:*Joseph Monte <hmjc66@gmail.com>

**Re: st: Average when -inrange()-***From:*Nick Cox <njcoxstata@gmail.com>

**Re: st: Average when -inrange()-***From:*Joseph Monte <hmjc66@gmail.com>

- Prev by Date:
**Re: st: Average when -inrange()-** - Next by Date:
**st: back transform coefficients** - Previous by thread:
**Re: st: Average when -inrange()-** - Next by thread:
**Re: st: Average when -inrange()-** - Index(es):