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

st: RE: summation


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: summation
Date   Fri, 9 Dec 2005 13:10:45 -0000

gen sumlength = . 
levelsof state, local(S) 

qui foreach s of local S { 
	su min if state == `s', meanonly 
	local min = r(min) 
	su max if state == `s', meanonly 
	local max = r(max) 
	su length if inrange(n, `min', `max'), meanonly 
	replace sumlength = r(sum) if state == `s' 
} 

Whether the variable -group- complicates 
your problem is not clear. 

Nick 
[email protected] 

A. Mushfiq Mobarak, Assistant Professor of Economics

> I post a part of my dataset below.  I would like to sum the variable
> "length" from n=min to n=max for each state, where n, min and 
> max are 3
> different variables.  I'm using Stata8SE. 
> 
> So, for example, for state=1, the answer I'm looking for would be:
> 
> summation (n going from 2 to 20) of "length', which equals
> 10+20+30+....+190 = 1900
> 
> For state=3, the answer would be summation (n going from 6 to 16) of
> length = 50+60+.+150 = 1100
> 
> For state=2, I would just get "." (missing)
> 
> This is a very large dataset, which is why I need to automate the
> summing procedure. I apologize in advance if the answer is obvious.  I
> will be grateful for any help or suggestions.  I subscribe to 
> the digest
> version, so copying the response to [email protected] 
> (hitting "reply
> all" rather than "reply" may also work) will help me get your response
> quicker.
> 
> group	n	min	max	state	length
> 3	2	2	20	1	10
> 3	3	.	.	2	20
> 3	4	2	20	1	30
> 3	5	2	20	1	40
> 3	6	6	16	3	50
> 3	7	2	20	1	60
> 3	8	6	16	3	70
> 3	9	2	20	1	80
> 3	10	6	16	3	90
> 3	11	2	20	1	100
> 3	12	6	16	3	110
> 3	13	2	20	1	120
> 3	14	6	16	3	130
> 3	15	2	20	1	140
> 3	16	6	16	3	150
> 3	17	17	25	4	160
> 3	18	2	20	1	170
> 3	19	17	25	4	180
> 3	20	2	20	1	190
> 3	21	17	25	4	200
> 3	22	22	28	5	210
> 3	23	17	25	6	220
> 3	24	22	28	5	230
> 3	25	17	25	6	240
> 3	26	22	28	5	250
> 3	27	.	.	7	260
> 3	28	22	28	5	270

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