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]
Re: st: create local macros named after the values of a categorial variable
From
Maarten buis <[email protected]>
To
[email protected]
Subject
Re: st: create local macros named after the values of a categorial variable
Date
Fri, 13 Aug 2010 13:58:01 +0000 (GMT)
--- On Fri, 13/8/10, Oliver Jones wrote:
> The -collapse- command is not going to work for me because
> of its vast need of memory, see my post:
> "RE: st: RE: collapse is too memory demanding"
> from Tue, 27 Jul 2010.
> The workaround is working but is quite slow, it took about
> 20 hours to complete. That's why I'm thinking of a way to
> program more efficient.
Maybe the variation on -collapse- in the example below solves
your problem?
*----------------- begin example ------------------
// create some example data
// the gr* variables are your group variables (like your city_code)
// the x* variables are the variables you want to count
drop _all
set obs 1000
gen long id = _n
gen gr1 = ceil(3*runiform())
gen gr2 = ceil(3*runiform())
gen gr3 = ceil(3*runiform())
gen x1 = runiform() < .2
gen x2 = runiform() < .5
gen x3 = runiform() < .7
// sort on your grouping variables and finaly your id
sort gr1 gr2 gr3 id
// create a variable indicating the last observation within a group
by gr1 gr2 gr3 : gen byte last = _n == _N
// store the counts of the first variable in a mata matrix counts
// notice that the number of rows in this matrix equals the number
// of groups not the number of observations
by gr1 gr2 gr3 : gen long count = sum(x1)
mata : counts = st_data(. , ("count"), "last")
// loop over the remaining variables
// if the variable names are not so nicely regular you can just
// loop over them using -foreach-
forvalues i = 2/3 {
by gr1 gr2 gr3 : replace count = sum(x`i')
mata : counts = counts, st_data(. , "count", "last")
}
// keep one observation per group and
// keep only your grouping variables
keep if last
keep gr1 gr2 gr3
// store the matrix of counts
mata
idx = st_addvar(("long","long", "long"), ("count1","count2", "count3"))
st_store(., idx, counts)
end
// display the results
list
*------------------- end example --------------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )
Hope this helps,
Maarten
--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany
http://www.maartenbuis.nl
--------------------------
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/