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: RE: selecting subsets of data based on date with which to calculate geonear (SSC)


From   "Ben Hoen" <bhoen@lbl.gov>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: selecting subsets of data based on date with which to calculate geonear (SSC)
Date   Fri, 21 Dec 2012 16:42:20 -0500

FYI, I just figured it out.  

I am not sure how elegant it is, but it works.

Ben
=============================
set seed 1234
sysuse auto, clear
gen ylat = 41+runiform()
gen xlong = -73-1*runiform()
gen date=16500+(int(500*runiform()))
format date %tdnn/dd/YY
g id=_n
keep id ylat xlong date price
order id price date ylat xlong


tempfile all case nnset tempset
save "`all'"
drop if id!=.
save "`tempset'", emptyok
use "`all'"
local N=_N

forvalues i = 1/`N' {
	preserve
	qui keep if id==`i'
	qui g testvar=`i'
	local maxdate=date
	local mindate=date-180
	qui save "`case'", replace
	qui use "`all'"
	qui keep if date>=`mindate' & date <=`maxdate'
	qui save "`nnset'", replace
	qui use "`case'"
	qui geonear id ylat xlong using `nnset', n(id xlong ylat) ignore
nearcount(2) miles
	append using "`tempset'"
	qui save "`tempset'", replace
	restore
	}
*
qui use "`tempset'"

==============================

Ben Hoen
LBNL
Office: 845-758-1896
Cell: 718-812-7589


-----Original Message-----
From: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Ben Hoen
Sent: Friday, December 21, 2012 3:04 PM
To: statalist@hsphsun2.harvard.edu
Subject: st: selecting subsets of data based on date with which to calculate
geonear (SSC)

Happy Holidays!

I am trying to find a way to loop through cases in a file, while selecting a
subset of that file (based on date) with which I would execute a
geonear(SSC) calculation with them. Specifically, for each case in the file,
I would like to select the nearest 5 neighbors that have a date which is
equal to or within the preceding 6 months.

I have some comfort with how to execute the geonear(SSC) part of it, but
seem to be lost with how to select subsets of data based on a particular
case.  

Here is my (feable) attempt to build this code.  I (clearly) nonplussed how
to do what I hope to do, but figure there are many examples.  If you have
any advice, links, etc, I would be greatly appreciative.

Best wishes for the holidays.  

Best,

Ben

=====================
set seed 1234
sysuse auto, clear
gen ylat = 41+runiform()  //to create a latitude
gen xlong = -73-1*runiform() //to create a longitude
gen date=16500+(int(500*runiform())) //to create dates that a within a few
years of each other
format date %tdnn/dd/YY
g id=_n //to create an id
keep id ylat xlong date price
order id price date ylat xlong

tempfile all nnset tempest  //to create temp files to be used in the loop
below
save "`all'" //saving the full dataset
clear
g emptyvar=. //to create a empty variable so I can save an empty dataset
save "`tempset'" // saving off the empty dataset

local N=_N //to establish the number of cases

forvalues i = 1/N { //I am trying here to begin a loop through the cases
	local maxdate=date
	local mindate=date-180
	keep if date>=`mindate' & date <=`maxdate' // my attempt to select a
group of cases
	save "`nnset'" // and to save off that group
	geonear id ylat xlong using `nnset', n(id xlong ylat) ignore
nearcount(5) miles //store info about the 5 nearest neighbors from the
subset
	append using "`tempset'" //append the case to the temp file
	}
*
===============================================



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/

*
*   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