[英]Generating sums of variables with loops in Stata
I'm having some problems with a loop that I'm trying to perform and probably with the syntax for generating the variable that I want. 我在尝试执行的循环以及在生成所需变量的语法方面遇到一些问题。
Putting in words, what I am trying to do make is a sum of a particular set of observations and storing each sum in a cell for a new variable. 简而言之,我要做的是将一组特定观察值的总和,并将每个总和存储在一个单元格中,用于一个新变量。 Here is an example of syntax that I used:
这是我使用的语法示例:
forvalues j=1/50 { replace x1 = sum(houses) if village==
j' & year==2010 }
forvalues j=1/50 { replace x1 = sum(houses) if village==
j' & year==2010 }
forvalues j=1/50 { replace x1 = sum(houses) if village==
& year==2010 }
gen x2=. forvalues j=1/50 { replace x2 = sum(houses) if village==
gen x2=. forvalues j=1/50 { replace x2 = sum(houses) if village==
j' & year==2011 }
gen x2=. forvalues j=1/50 { replace x2 = sum(houses) if village==
j' & year==2011 }
gen x2=. forvalues j=1/50 { replace x2 = sum(houses) if village==
& year==2011 }
gen x3 =. forvalues j=1/50 { replace x3 = sum(houses) if village==
gen x3 =. forvalues j=1/50 { replace x3 = sum(houses) if village==
j' & year==2012 }
gen x3 =. forvalues j=1/50 { replace x3 = sum(houses) if village==
j' & year==2012 }
gen x3 =. forvalues j=1/50 { replace x3 = sum(houses) if village==
& year==2012 }
This is from a dataset with more than 4000 observations. 这来自具有4000多个观测值的数据集。 So, for each particular j, if I were successful with the code above, I would get an unique observation for each j (what I want to obtain), but I'm not obtaining this -- which is a sum of all houses, conditioned with the year and village;
因此,对于每个特定的j,如果我成功使用了上面的代码,我将获得每个j的唯一观测值(我想要获取的值),但我没有得到-这是所有房屋的总和,以年份和村庄为条件; the total sum of houses per village in each year.
每年每个村庄的房屋总数。 I would greatly appreciate if someone could help me obtain one particular observation for each j in each variable.
如果有人可以帮助我为每个变量中的每个j获得一个特定的观察值,我将不胜感激。
sum()
will return a running sum, so that is probably not what you want. sum()
将返回运行总和,因此可能不是您想要的。 This type of problem is usually much easier to solve with the by: prefix in combination with the egen command. 使用by:前缀结合egen命令通常可以更轻松地解决此类问题。 The one line command below will give you the total number of houses per village and year:
下面的一行命令将为您提供每个村庄和每年的房屋总数:
bys village year: egen Nhouses = total(houses)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.