How do I create an index, ID or key variable?
First situation
Question
I would like to create a new variable (an ID variable) which would number each case in my data, starting with 1 and ending with n, the number of cases in the file. How can I do that?
Answer
- Using syntax: COMPUTE id=$CASENUM.
(If nothing happens, select TRANSFORM>RUN PENDING TRANSFORM)
- Using the menu: select TRANSFORM>COMPUTE then enter id in the Target Variable text box and $casenum in the Numeric Expression text box. Click OK.(If nothing happens, select TRANSFORM>RUN PENDING TRANSFORM)
Note that $CASENUM is a system variable. The easiest method to find the list of all system variables is to open SPSS Help and search for "$casenum".
Second situation
Question
What if I do not have any data in my data file but I would still like to create an ID variable with values between 1 and 10.
Answer
This cannot be done using the menu, you need syntax:
- Using syntax:
INPUT PROGRAM. LOOP id=1 TO 10. END CASE. END LOOP. END FILE. END INPUT PROGRAM. LIST.
Third situation
Question
I have many cases with the same ID and would like to create a new variable where I would number consecutively those cases (within each ID).
Answer
See the syntax "Number consecutively cases with the same id.sps".
...