Search code examples
loopsif-statementstataspss

Translate a Stata loop into a SPSS loop


Could anyone help with the translation of the following Stata code? I need this code for further analysis in 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:

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?


Solution

  • This particular use of egen in Stata can be replicated in SPSS by using the AGGREGATE command. Using Nick Cox's revised Stata code:

    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:

    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.