Search code examples
loopsstataspss

Translation of a Stata loop into SPSS


Could anyone help with the translation of the following Stata code? I need this code for further analysis in SPSS.

  gen quintile=.
  forvalues i=1/2 {
  xtile quintile`i'= income if income_num==`i', nq(5)
  replace quintile=quintile`i' if income_num==`i'
  drop quintile`i'
    }

I tried to solve the problem with the following SPSS code:

COMPUTE quintile=($sysmis).

LOOP if #i = 1 to 2.
DO IF (income_num= #i).
FREQUENCIES income 
/ntiles (5) into quintile#i.
DROP = quintile #i.
END LOOP.
EXECUTE.

But this code appears to be wrong. There are errors in the whole SPSS syntax. First, I would like to know the logical structure of the SPSS Loop in this special case. Second, I ask how to combine this special code with the "Frequencies" command and the "Drop" command.

Do you have any idea how to resolve the problem?


The following is a list of the occurring errors.

FREQUENCIES: LOOP has no effect on this command.

FREQUENCIES: The transformations program contains an unclosed LOOP, DO IF, or complex file structure. Use the level-of-control shown to the left of the SPSS Statistics commands to determine the range of LOOPs and DO IFs.

DROP: LOOP has no effect on this command. DROP: The first word in the line is not recognized as an SPSS Statistics command.

END LOOP: The END LOOP command does not follow an unclosed LOOP command. Maybe the LOOP command was not recognized because of an error. Use the level-of-control shown to the left of the SPSS Statistics commands to determine the range of LOOPs and DO IFs.


Solution

  • You may need to take a bit of time to familiarize yourself with SPSS code. (You can't just make things up and expect them to work!) There is no option into on the NTILES subcommand of frequencies. FREQUENCIES simply outputs tables, it does not transform the data in any way.

    See the command RANK for what you want, something like:

    RANK VARIABLES = income BY income_num /NTILES(5) INTO quantile.
    

    No looping necessary.