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]

From |
"Ben Hoen" <bhoen@lbl.gov> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: Easy Question? Counting cases based on a "target" case |

Date |
Wed, 26 Dec 2012 13:05:52 -0500 |

I want to perform a function that I think would be easy but I can't wrap my head around how to perform it without looping through each case. I want to create a count of the number of records in the file that meet a certain criteria based on a respective case's value. So for example using the auto dataset: *====================begin sysuse auto, clear g id=_n egen nearprice2000=count(id) if... //count the number of other cases in the dataset if the price of the car is within $2000 of the price of this cases' (i.e., target) car's price *====================end The egen command is how I thought I would resolve this, but I can't figure it out exactly. The nearprice2000 would equal the count for each case of the number of other cases in the dataset that have a price that is either +/- $2000 from the particular case's price. So if the full dataset had only 5 prices: 1900, 2500, 4000, 6500, and 10000, their respective nearprice200 values would be: 2, 3, 2, 2, and 1 (if itself would be included in the count) or 1, 2, 1, 1, and 0 (if itself would NOT be included in the count) I might be able to do this by looping through the cases, but I know that is not encouraged by other more experienced users. Any advice would be greatly appreciated. Ben Ben Hoen Principal Research Associate Lawrence Berkeley National Laboratory Office: 845-758-1896 Cell: 718-812-7589 bhoen@lbl.gov http://emp.lbl.gov/staff/ben-hoen Visit our publications at: http://emp.lbl.gov/publications * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**st: RE: Easy Question? Counting cases based on a "target" case***From:*"David Radwin" <dradwin@mprinc.com>

- Prev by Date:
**st: create one factor level from two existing** - Next by Date:
**st: RE: create one factor level from two existing** - Previous by thread:
**st: create one factor level from two existing** - Next by thread:
**st: RE: Easy Question? Counting cases based on a "target" case** - Index(es):