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

st: RE: aggregate means of 113 grouped variables


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: aggregate means of 113 grouped variables
Date   Thu, 6 May 2004 12:25:56 +0100

Long term, this data structure may prove
awkward and some reshape (or -reshape-) 
may be worthwhile. 

With your existing structure, and if 
I understand this correctly: 

1. Take the variable list and extract
substr("<varname>",3,4)) in each case. 

foreach v of var * { 
	local mid = substr("`v'",3,4) 
	local mids "`mids' `mid'" 
} 

2. That list will have repeated values, 
so we slim it down: 

local mids : list uniq mids 

and sort it at the same time 

local mids : list sort mids 

(see help on -macrolists-) 

3. Now we cycle through those 
middle bits: 

qui foreach m of local mids { 
	unab this : ??`m'?? 
	local nthis : word count `this' 
	if `nthis' { 
		gen m`m' = 0 
		foreach v of local this { 
			su `v', meanonly 
			replace m`m' = m`m' + r(mean) 
		} 
		replace m`m' = m`m' / `nthis' 
	} 
} 

(not tested!) 

Dietrich Dr�ner
 
> my data set contains 113 variables that belong to 62 cases. 
> There are 1 to 5
> variables per case.
> I need to calculate the mean of the means of these 1 to 5 
> variables for each
> of my 62 cases.
> 
> -----------------------------------------
> A possible code for one case with 3 variables looks like this:
> 
> sum sd0032i1
> local x=r(mean)
> sum sd0032i3
> local y=r(mean)
> sum sd0032i4
> local z=r(mean)
> 
> gen msd0032=(`x'+`y'+`z')/3
> 
> --------------------------------------
> 
> The 3rd to 6th letter of the variable names is identical for 
> the variables
> of each case, the last letter distinguishes the 1 to 5 
> variables with an
> ascending order but unfortunately sometimes with "gaps" like 
> in the case
> above.
> 
> My problem is: how can I generalise this - or an equivalent - 
> code to be
> repeated 62 times with a varying number of variables?
> 
> 

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index