* Computing New Data Values.
*******************************.
get file="y:\spss\data\employee data.sav".
* The compute command is the starting point for creating new data
values in SPSS.
compute totalraises = salary - salbegin.
* A "transformation" command, transformations pending . Most commands that
write numbers into a data set are transformation commands.
* Actual execution of the command waits for an executable ("procedural") command. See
the SPSS "Command Syntax Reference", Appendix B, "Program States".
execute.
formats totalraises (dollar7).
variable labels totalraises "Career total raises".
compute jobyears = jobtime/12.
compute raisesperyear = (salary - salbegin)/jobyears /* Total raises, averaged */ .
formats raisesperyear (dollar7).
compute avgpctraise = raisesperyear/salbegin.
compute annualraise = (salary/salbegin)**(1/trunc(jobyears)) -1 /* Actual raises before compounding */ .
* Multiple computes are executed in one pass through the data.
* Compute replaces values in existing variables.
t-test groups=gender("m", "f") /* Any statistical command is "procedural" */
/variables=salary salbegin jobtime totalraises raisesperyear avgpctraise annualraise.
* Raises are different, "percentaged" raises are not.
* There are many available functions, see the "Command Syntax Reference" section
on "Transformation Expressions".
* SPSS has some handy special variables (hidden system variables) like $casenum and $time .
* In a data set without an ID variable, you could create one with this.
compute newid = $casenum.
execute.
* Often you want to store a variable like this, or zip code or FIPS as a string
variable, because the numbers have no mathematical meaning, they are
only category labels.
* In SPSS creating a string variable requires two steps. If you try to just go
ahead an compute a string value, you get an error, e.g.
compute newid2 = string($casenum, f3.0).
execute.
* Whenever SPSS encounters a new variable name, it automatically assumes the
variable should be numeric, so newid2 is assumed to be numeric yet we are
trying to give it string data ... ERROR.
* Instead you need to declare newid2 a string variable first. Then you can assign
string values to it.
string newid2 (a3).
compute newid2 = string($casenum, f3.0).
execute.
* Note that we make use of a couple of "formats" here (the "a3" and "f3.0"). We'll
come back to formats, later.
* Using string concatenation to create a cross-classified factor variable.
string genderjob (a3).
compute genderjob = concat(gender, string(jobcat, f1.0)).
means variables=annualraise by genderjob.
unianova annualraise by genderjob
/posthoc=genderjob (scheffe).
* Dates (and times).
* Dates are stored as "date-times" in SPSS, the number of seconds that have
gone by since midnight of 14 October 1584 (the beginning of the Gregorian
calendar). Formats help us interpret these massive numbers.
* Storing dates in this way makes it very easy for us to do date arithmatic, or
extract date components (month, week, year, etc.).
* $time is the current date & time.
* To compute an age given a birthdate.
compute age = trunc(($time - bdate)/(60*60*24*365.25)).
* We divide by the number of seconds in a year, and truncate to get age in years.
means variables=age by jobcat.
* The datediff() function gives you a cleaner (and slightly more precise) way
to accomplish the same task.
compute age2=datediff($time,bdate,"year").
means variables=age by jobcat.
* There are a number of handy date creation functions, like date.mdy(). It might
be more appropriate to calculate age at the time the data was collected here.
compute age2=datediff(date.mdy(1,20,1995),bdate,"year").
t-test groups=gender("m", "f")
/variables=age age2.
* Note although the means are different in age and age2, everything else about the t-tests is the same.