我的数据看起来有点像这样。第一行是变量名,其余是我的值。year描述选举的年份,nutsid是一种区域标识符,nutsname是该区域的名称。现在我想把重点放在接下来的三个方面:spo、ovp和fpo是选举中政党的名字。我希望将它们合并到一个名为party的变量中,并保持我现在的长格式。
| year nutsid nutsname spo ovp fpo|
1. | 2008 AT11 Burgenland 73565 52531 29812|
2. | 1990 AT11 Burgenland 88547 62675 19508|
3. etc到目前为止,我尝试的是使用reshape命令首先将其重新格式化为宽格式,然后用以下方式重命名它们,比如p_spo、p_ovp、p_fpo
reshape long p_, i(nutsid) j(year party) string我不能说这是一个聪明的想法,也不能说它有效,因为它只是给了我一个名为year的新id,它的值"party"一遍又一遍地写在它下面。
但我在想,我是否还应该使用另一个命令来获取我需要的信息:
| year nutsid nutsname party votes|
1. | 2008 AT11 Burgenland spo 73565|
2. | 2008 AT11 Burgenland ovp 52531|
3. | 2008 AT11 Burgenland fpo 29812|
4. | 1990 AT11 Burgenland spo 88547|
5. | 1990 AT11 Burgenland ovp 62675|
6. | 1990 AT11 Burgenland fpo 19508|
7. etc发布于 2016-07-21 10:21:48
这里的一些次要细节是混淆或不明确的:
reshape命令要求spo ovp fpo be renamed到p_spo p_ovp p_fpo 在reshape之前;此重命名不遵循reshape。也就是说,您想要的是一个简单的reshape:
clear
input year str4 nutsid str10 nutsname spo ovp fpo
2008 AT11 Burgenland 73565 52531 29812
1990 AT11 Burgenland 88547 62675 19508
end
rename (spo ovp fpo) (votes=)
reshape long votes, i(nutsid year) j(party) string
list, sepby(nutsid year)
+--------------------------------------------+
| nutsid year party nutsname votes |
|--------------------------------------------|
1. | AT11 1990 fpo Burgenland 19508 |
2. | AT11 1990 ovp Burgenland 62675 |
3. | AT11 1990 spo Burgenland 88547 |
|--------------------------------------------|
4. | AT11 2008 fpo Burgenland 29812 |
5. | AT11 2008 ovp Burgenland 52531 |
6. | AT11 2008 spo Burgenland 73565 |
+--------------------------------------------+在这个数据视图中,您有两个所谓的i变量和一个j变量。
请注意这里使用的input代码提供了一个数据示例,该示例将在不需要您的示例所需的工程的情况下运行。您可以使用dataex安装命令ssc inst dataex,这样做也很容易。
https://stackoverflow.com/questions/38499536
复制相似问题