Bookmark and Share

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]

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


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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index