简体   繁体   English

将Stata循环转换为SPSS循环

[英]Translate a Stata loop into a SPSS loop

Could anyone help with the translation of the following Stata code? 有人可以帮忙翻译以下Stata代码吗? I need this code for further analysis in SPSS. 我需要此代码以在SPSS中进行进一步分析。

if year<1990 {
    bysort country year ID: egen sum080=sum(PY080g)
    gen hydisp=(HY020+sum080)*HY025
}
else gen hydisp=HY020*HY025

I tried to solve the problem with the following SPSS code: 我尝试使用以下SPSS代码解决问题:

DO IF year<1990.
   SORT CASES BY country year ID.
   COMPUTE sum080 = SUM(PY080g).
   COMPUTE hydisp=(HY020+sum080)*HY025.
ELSE. 
   COMPUTE hydisp=HY020*HY025.
END IF.
EXECUTE.

But this code appears to be wrong. 但是此代码似乎是错误的。 Do you have any idea how to resolve the problem? 您知道如何解决该问题吗?

This is in no sense an answer on SPSS code, but it makes a point that would not go well in a comment. 这绝不是SPSS代码的答案,但它提出的观点在注释中不会很好。

The Stata code Stata代码

if year < 1990 {
    bysort country year ID: egen sum080=sum(PY080g)
    gen hydisp=(HY020+sum080)*HY025
}
else gen hydisp=HY020*HY025

would get interpreted as 将被解释为

if year[1] < 1990 {
    bysort country year ID: egen sum080=sum(PY080g)
    gen hydisp=(HY020+sum080)*HY025
}
else gen hydisp=HY020*HY025

ie the branching is on the value of year in the first observation (case, record). 即分支上的价值year第一观察(情况下,记录)。 The if command and the if qualifier are quite different constructs. if命令和if限定符是完全不同的结构。 It seems much more likely that the code desired is something like 所需的代码似乎更像是

bysort country year ID: egen sum080 = sum(PY080g)
gen hydisp = (HY020 + sum080) * HY025 if year < 1990 
replace hydisp = HY020 * HY025 if year >= 1990 

or 要么

bysort country year ID: egen sum080 = sum(PY080g)
gen hydisp = cond(year < 1990, (HY020 + sum080) * HY025, HY020 * HY025) 

The OP's comment that the code appears to be wrong is a poor problem report. OP认为代码似乎有误的评论是不良的问题报告。 What is wrong precisely? 到底是什么问题? It may be nothing more than inability to replicate the results gained in Stata, which would not be surprising as the Stata code is almost certainly not what is intended. 可能仅仅是无法复制从Stata获得的结果而已,这并不奇怪,因为Stata代码几乎肯定不是预期的。 It seems unlikely that the first observation is special, but rather that the calculation should be carried out for all observations according to the value of year 第一个观测值似乎不太可能是特殊的,而是应该根据year的值对所有观测值进行计算

Detail: sum() as an egen function is undocumented in favour of total() , but the syntax remains legal. 详细信息: sum()作为本egen函数未作记录,而支持total() ,但语法仍然合法。

Detail: The Stata code here would not be considered a loop just because there is a tacit loop over observations. 详细信息:此处的Stata代码不会仅仅因为观测值存在默认循环而被视为循环。

This particular use of egen in Stata can be replicated in SPSS by using the AGGREGATE command. 可以使用AGGREGATE命令在SPSS中复制对Stata中egen这种特殊使用。 Using Nick Cox's revised Stata code: 使用Nick Cox修改后的Stata代码:

bysort country year ID: egen sum080 = sum(PY080g)
gen hydisp = (HY020 + sum080) * HY025 if year < 1990 
replace hydisp = HY020 * HY025 if year >= 1990 

A synonymous set of code in SPSS would be: SPSS中的同义代码集为:

AGGREGATE OUTFILE=* MODE=ADDVARIABLES
  /BREAK = country year ID
  /sum080 = SUM(PY080g).
DO IF Year < 1990.
  COMPUTE hydisp = (HY020+sum080)*HY025.
ELSE.
  COMPUTE hydisp = HY020*HY025.
END IF.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM