Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Counting unique values over moving time windows


From   smerryman@kc.rr.com
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Counting unique values over moving time windows
Date   Wed, 29 Jun 2005 13:24:17 -0500

Here is one way, using -inspect- to count the unique values and -forv- 
to loop across observations:

clear
set obs 2
gen id = _n
expand 20
bysort id: gen time = _n
by id: gen  x = round(10*uniform(),1)


qui {
gen count =.
forv k = 1/2 { 
	local j = 1
	forv i = 5/20 {
		inspect x if  time >=`j' & time <= `i' & id == `k'
		replace count =r(N_unique) if time == `i' & id == `k'
		local ++j
	}
}
}

Hope this helps,
Scott


----- Original Message -----
From: Arik <ericl@rocketmail.com>
Date: Wednesday, June 29, 2005 12:10 pm
Subject: st: Counting unique values over moving time windows

> I am working with panel data and am trying to create a
> variable that will count the number of unique values
> an existing variable has over overlapping moving
> windows (i.e. I have a list of products sold to
> customers and wish to find the total number of
> customers over 5-year moving windows). This seems to
> fall between the stata commands UNIQUE and MOVSUMM,
> but I can't really think of a syntax that will provide
> the results I'm looking for. I would appreciate any
> help you may offer.
> Arik
> 
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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