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: Easy Question? Counting cases based on a "target" case


From   "Ben Hoen" <[email protected]>
To   <[email protected]>
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
[email protected]
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/


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