SPSS uses a handful of hidden variables for keeping track of things. These so called system variables have names starting with "$" and can be useful in some cases. The most important one is $casenum, the number shown next to each case in the data editor window.
SPSS $casenumSPSS $casenum
In the left outline of the data editor window (under
$casenum. To some extent we can use
$casenum in normal SPSS commands. We can also convert it into a regular variable. This can be used to create a unique row id if this is absent. The syntax below demonstrates this.
SPSS Syntax Example 1
data list free/age.
14 19 29 30 31
*2. Use $casenum in regular command.
compute first_case = $casenum eq 1.
*3. Convert $casenum to normal variable.
compute id = $casenum.
SPSS Select If and $casenum
SELECT IF with
$casenum can be tricky. This is because SPSS uses
$casenum to keep track of cases during procedures. If cases are deleted during a procedure, their
$casenum may shift to a different case. Like so,
SELECT IF $casenum GT 1. will delete all cases in the data. What happens, is this
- SPSS starts at the first case. Since its
$casenumis not greater than one, it's deleted.
- The second case now shifts up one row and is thus assigned
- Since the second case's
$casenumis not greater than one (anymore), it's deleted as well.
- Now the third case shifts up and gets
$casenum1. And so on.
A second system variable that may sometimes be useful is $time. This is hold the current date and time. As with $casenum, it can be used straight away, for example in
DATEDIFF. Alternatively, its value can be passed into a regular variable as demonstrated by the final syntax example.
SPSS Syntax Example 2
data list free/now.
*2. Compute now = current date and time.
compute now = $TIME.
*3. Display now as datetime variable.
SPSS uses more system variables than
$time. Consult the command syntax reference for a complete overview.