Syntax vs. Point & Click Some Advantages of Syntax from Alan Taylor, Macquarie University http://www.psy.mq.edu.au/psystat/documents/Using_Syntax.htm When you have some familiarity with the syntax commands, it is often a lot quicker and less cumbersome to type the commands rather than go through a complicated sequence of mouse clicks. When you have written a set of commands out once, it is very easy to repeat them, either during the same session, or, if you have saved the contents of the Syntax Window, at a later time. The commands you have entered into the Syntax Window during an analysis or a series of analyses form a valuable record of the steps you've taken. There are some things which you can do using syntax which cannot be done with pointand-click alone. Syntax works with all versions of SPSS, not just with SPSS for Windows. Some Syntax Basics All SPSS Syntax files are text files and may be created/edited with any TEXT editor such as Notepad, Notepad++ (a free much better version of the built in Notepad), or the SPSS Syntax Editor. Syntax files all have the file type of .sps. To create one in SPSS, click filenewsyntax and the syntax editor will open. To open an existing one, click fileopensyntax and browse to the Syntax File (.sps) you wish to open. To execute the Syntax File, while in the Syntax Editor, click runall. Then What can you do in SYNTAX Specifically? Set up a Data Set Archive – Have You Ever created a data set, put it away then got back to it a year or two later and found you didn’t have a clue as to what was in it? made an error in an SPSS data file, then accidently saved it or done a number of things and decided to return to what it was initially after saving it? wished you had the data stored in a form you could read with a program other than SPSS? Setting up data in Syntax is the answer . VERY Simple Syntax Operations Getting descriptive statistics for all your numerical variables: Open syntax window Enter “Descriptives ALL.” (don’t enter the “) Runall Tired of getting all possible correlations when all you want is to correlate age with four other variables (v1, v2, v3, v4)? Open syntax window Enter “Correlate age with v1 to v4.” (don’t enter the “) Runall Note that the variables on the left will be rows and those on the right columns. In the example, the output would have one row for age and four columns for v1, v2, v3, v4. You want to compute a scales score that is the sum of the items v1, v2, v3, v5, v6. Open new Syntax window. COMPUTE scale1 = sum(v1, v3, v3, v5, v6) or sum (v1 to v3, v5, v3). A new variable called scale1 will be created containing that sum. If you wanted the average of the same variable replace sum with mean. The second line is simply EXECUTE. Then runall. What about if you expected missing data for one or more of those variables? Decide the maximum number of variables with missing values you will tolerate before setting the scale to the system missing value. For this example we will allow 2 missing values before setting scale1 to the system missing value. You will need to do this one with the “mean” function. COMPUTE scale1 = mean.2(v1, v3, v3, v5, v6). The results will be the mean of the items without missing data unless more that 2 of the variables are missing for that case. If there are more than 2, then scale1 will be assigned the system missing value. A Little More Sophistication! In Syntax, you can do substantial data manipulation. Here are some examples: Finding bad data: *(Q) How can I find invalid entries in my data file. *(A) Raynald Levesque on 2002/07/28. DATA LIST LIST /id var1 var2 var3. BEGIN DATA 1 2 8 14.23 2 3 4 9.21 3 5 1 14 4 0 2 13.26 END DATA. SAVE OUTFILE='c:\temp\temp.sav' * Suppose the valid entries for var1 and var2 are 1, 2,3,4 & 5. * and the valid entries for var3 are any number between 10 and 15. *The following syntax flags the incorrect entries. DO REPEAT flag=flag1 flag2 /var=var1 var2. COMPUTE flag=~ANY(var,1,2,3,4,5). END REPEAT. COMPUTE flag3=~RANGE(var3,10,15). EXECUTE. SELECT IF SUM(flag1 TO flag3)>0. LIST. *The result of the list command is: ID VAR1 VAR2 VAR3 FLAG1 1.00 2.00 4.00 2.00 3.00 .00 8.00 4.00 2.00 14.23 9.21 13.26 Number of cases read: 3 .00 .00 1.00 FLAG2 1.00 .00 .00 FLAG3 .00 1.00 .00 Number of cases listed: 3 Flag2 =1 means that case 1 has an incorrect value for var2. Flag3 =1 means that case 2 has an incorrect value for var3. Flag1 =1 means that case 4 has an incorrect value for var1. Or how do you easily resize multiple columns in data editor? * (Q) Many columns in the data editor are too large. How could I easily reduce the size of all these columns? * (A) Posted to SPSSX-L by Ray on 2002/04/03. * The following will set all columns to 5 characters width. DO REPEAT var=ALL. VARIABLE WIDTH var(5). END REPEAT. String variables are automatically left justified. What if you want one to be right justified: *(B) Does SPSS have a bug? String variables will not right align in the data editor. *(A) Posted by Raynald Levesque to SPSSX-L on 2002/11/21. * Say a variable str1 is an A8 string variable. * When the value assigned to str1 has less than 8 characters, SPSS pads the right of the string with blanks. This is why when the "right align" feature of the variable is set, the data editor does not *appear* to be right aligned. In fact cells content are right aligned but blanks at the end produce the wrong impression. This is easy to fix as is shown by the following syntax: DATA LIST LIST /str1(A8). BEGIN DATA a ab abc abcd abcde abcdef abcdefg abcdefgh END DATA. LIST. COMPUTE str1=LPAD(RTRIM(str1),8). VARIABLE ALIGNMENT str1(RIGHT). EXECUTE. *The compute statement removes any trailing blanks and replaces them by beginning blanks. That way, data are right align in the data viewer. Calculations in Syntax Here are some examples of SPSS Syntax Files that do various analyses: Canonical Correlation must be done in Syntax – it needs two files: Canonical Correlation Syntax.SPS Canonical correlation.sps Finding the # of Factors for Factor Analysis Parallel Analysis.sps MAP.sps (Velicer’s Minimum Average Partials Here are some others that do specific calculations: Normalize a variable; i.e., force a normal distribution: Normalize.sps Calculate Age in years & fraction of years: Age.sps. Create a new variable which is the selected variable/the maximum value of the selected variable: Divide by Max.sps Finding More Useful Syntax Files There are numerous sources for FREE SPSS Syntax files: Probably the best source I could find was: http://pages.infinit.net/rlevesqu/SampleSyntax.htm In fact most of the Syntax Files I’ve shown you today were from this source. Another good link: http://www.spsstools.net/SampleSyntax.htm#MultipleResp There are many more. Winding it Up. I have all the Syntax Files I discussed + a few more with me. If you want them and have a USB drive with you, I can give them to you now. If not, send me an email and I’ll send them to you. My email address is: Charles.Burdsal@wichita.edu Questions – Discussion – etc.