rssunt.gif (12308 bytes)


Creation date: 06/16/2000
Authored by: Karl Ho

Question:

How to convert the case number into a variable in SPSS?

Answer:

SPSS has a system variable $CASENUM representing the number of the permanent cases read up to the last valid case. This variable is, however, not available for procedures except transformation.  The following example converts this system variable into a permanent variable VNAME with a preceding string Q: 

string #id vname (a3).
compute #id=lpad(ltrim(string($casenum,f2.0)),2,'0').
compute vname=concat('Q',#id).
exec.

The #ID variable is a scratch variable that temporarily exists for creating the VNAME variable.  This applies to an N of up to 99 cases. Leading zero is added to the single digit cases.  It could be modified to add leading zeros in case column width of the new variable is greater than 3.

The resulting VNAME looks like:

Q01
Q02
Q03
Q04
Q05
Q06
Q07
Q08
Q09
Q10
Q11
Q12
Q13
Q14
Q15
Q16
Q17
Q18
Q19
Q20
.
.
.
.

BACK | MAIN

Last updated: 01/18/06 by Karl Ho