SPSS, STATA, and SAS:
SPSS, STATA, and SAS:
Flavours of Statistical Software
A. Michelle Edwards, Ph.D.
Ontario DLI Training
April 14, 2005
Kingston, ON
Data background
CANADIAN COMMUNITY HEALTH SURVEY (CCHS) CYCLE 2.1 (2003)
Summary:
“The Canadian Community Health Survey (CCHS) is a cross-sectional survey that collects information related to health status, health care utilization and health determinants for the Canadian population. The CCHS operates on a two-year collection cycle. The first year of the survey cycle “.1” is a large sample, general population health survey, designed to provide reliable estimates at the health region level. The second year of the survey cycle “.2” is a smaller survey designed to provide provincial level results on specific focused health topics.
This Microdata File contains data collected in the third year of collection for the CCHS (Cycle 2.1). Information was collected between January 2003 and December 2003, for 126 health regions, covering all provinces and territories. The CCHS (Cycle 2.1) collects responses from persons aged 12 or older, living in private occupied dwellings. Excluded from the sampling frame are individuals living on Indian Reserves and on Crown Lands, institutional residents, full-time members of the Canadian Armed Forces, and residents of certain remote regions. The CCHS covers approximately 98% of the Canadian population aged 12 and over.”
Source: CCHS Cycle 2.1 2003 User Guide.
Datafile information:
The complete PUMF datafile contains 1068 variables and 134072 cases. For the purposes of this workshop the dataset has been trimmed to 8 variables and only contains data pertaining to Nova Scotia, Ontario and Alberta.
The data was originally provided in an SPSS and a SAS format along with SPSS and SAS data definition statements. For the purposes of this exercise you will be provided with a CSV (comma-separated values) file.
I’ve also included the portions of the data dictionary that pertain to the variables used in this exercise. Please note to view the entire data, codebook and associated data – you will need to access the DLI site or a DLI member institution.
Codebook information:
[pic]
[pic]
[pic]
[pic][pic][pic]
Source: Data Dictionary CCHS Cycle 2.1 (2003).
Downloading and Saving the CCHS Dataset to the Desktop
Please go to the following webpage to download the CSV dataset called cchs2003.csv
There are 2 ways to save the dataset to your desktop:
1. Right-click on the link – select Save Target as.. or Save Link as… - Select Desktop and save.
2. Follow link (to view file) – Use the File – Save as.. on the Web Browser software. Please note that if you use this method you must Change Save as Type… to Text. If you forget this step you will saving an HTML page which will not open in the Statistical computing packages. Save the file to the Desktop.
Viewing the CSV file
Now that we have our file – let’s take a look at the contents of the file to get a feeling for it and to see what it looks like. Let’s open the file in Notepad.
Open Notepad (Start – Run – type Notepad – OK).
File – Open – navigate to the Desktop.
Note that your file is not here – you need to change file type to All Files.
Select CSV file and Open.
[pic]
The file contains a number of variable names and a bunch of numbers. To decipher the variable names we need to correspond these with the codebook sections inlcuded above. The numbers have no meaning unless we have the codebook handy. To perform any sort of analysis or summary – Notepad is not going to do it….
1. SPSS
Open the SPSS Program.
[pic]
Similar look and feel to Excel. Spreadsheet type look, icons at the top and menu bar at the very top.
Think of the SPSS dataview as a container for the data. You cannot manipulate the data in this window as you would an Excel spreadsheet.
Open cchs2003.csv in SPSS
[pic]
Navigate to your Desktop – Change Files of type to All Files(*.*) – Select cchs2003.csv - Open
[pic]
SPSS immediately recognizes that the cchs2003.csv file is not an SPSS datafile. As a result it opens up the Import Wizard and walks you through 6 steps to open the datafile.
[pic]
Step 1 - Allows you to select a format for data that you’ve already read in. Chicken and the egg concept. This window also shows you what your data looks like.
[pic]
Step 2 – Select how your variables are arranged. In this example our file is delimited – there’s a comma separating our variables. We also have the variable names listed at the top of our file.
[pic]
Step 3 – If there are variable names at the top of your file – your data must start on line 2. If you want to skip the first 10 lines of data – you can tell SPSS to start reading the data on line 11.
In this example each line represents a case. However, in some situations this may not be the case. For example if someone has entered the data in the following format:
M 12 S F 13 S
F 35 M F 45 D
M 78 W M 50 M
Where each line represents the Gender of an individual, their age, their marital status, the gender of another individual, their age, and their marital status.
In this situation – the number of variables represents a case – in the little example above it would be three variables for each case.
[pic]
Step 4 – deciding what the appropriate delimiters are. In most cases SPSS will do its best to determine the best delimiter or combination of delimiters. In the cchs2003.csv file SPSS has decided that a Comma and a Space are the delimiters. This would result in a wrong file configuration. Since we know that the file was comma-separated – let’s use the Comma only. In situations where the delimiter is unknown, you can play around with the selection of delimiters until the data look correct.
[pic]
Step 5 – In our example we have variable names. If you import a file without variable names – this is the window where these can be added or changed. Select a column – the variable name and data format boxes become available. Please note that above the Numeric there is an option of Do Not Import.
[pic]
Step 6 – The final step. Click Finish and the data is now in SPSS.
[pic]
BUT – All we have now is the same data we saw in Notepad in columns – a little neater but that’s about it…
Goal – is to add variable labels and value labels.
In SPSS – at the bottom of the screen there are 2 tabs – one is the DataView (which we have been in) and the other is the VariableView.
[pic]
If you select the VariableView tab you will be presented with a table where we can enter information regarding the variables.
[pic]
All variables in our dataset are of the Numeric format, with varying widths and some came in with decimal places while others came without. All of these features can be changed.
Variable Labels
Let’s add a label for geocgpr as Province – Under the column called Label – type the label you would like to see associated with the variable. It becomes quite handy when working with surveys to add the question under the label.
Here are the labels for the 8 variables in our dataset:
Geocgprv – Province
DHHC_SEX - Gender
DHHCGMS – Marital Status
GENCDHDI – Self-Rated Health
GENCDMHI – Self-Rated Mental Health
CIHC_8A – Health Improvement – More exercise
CIHC_8B – Health Improvement – Lose weight
WTSC_M – Master weights
Please note that instead of labels you can enter the question text.
[pic]
Please do not forget to save your dataset along the way. To save the file – File -> Save as… Navigate to the Desktop -> save as cchs2003.sav SAVE
SPSS datafiles are saved as .sav files.
Value Labels
So now we know what the variable names represent BUT we still do not know what the values within the variables represent. We can use our codebook to view these – but it would be handy to add these into the SPSS program.
Select the cell under the Values column for the geocgprv variable
[pic]
Click on the grey square with the 3 dots to obtain the value label dialogue box.
[pic]
For the geocgprv variable – we are given the following codes:
10 – Newfoundland and Labrador
11 – Prince Edward Island
12 – Nova Scotia
13 – New Brunswick
24 – Quebec
35 – Ontario
46 – Manitoba
47 – Saskatchewan
48 – Alberta
59 – British Columbia
60 – Yukon/NWT/Nunavut
We already know that the data we’re working with only pertains to Nova Scotia, Ontario and Alberta. So we will only be adding these value labels.
Let’s add the value label for Nova Scotia first.
To add this to SPSS – type 12 in the Value box, and Nova Scotia in the Value label box. Then click on Add to add this to the dataset. Work through the same steps for Ontario and Alberta labels.
[pic]
Click OK when finished.
The variable view now looks like this…
[pic]
You can enter as many or as few of the values as you wish… When you switch back to the DataView – some people would rather see the labels and not the values. To accomplish this – in DataView click on the [pic] icon at the top right-hand side of your screen.
[pic]
Now we know what the variable names represent as well as the variable value labels. We’re ready to do some analysis now…
Survey Weights
“The principle behind estimation in a probability sample such as the CCHS Cycle 2.1 is that each person in the sample "represents", besides himself or herself, several other persons not in the sample. For example, in a simple random 2% sample of the population, each person in the sample represents 50 persons in the population. In the terminology used here, it can be said that each person has a weight of 50.
The weighting phase is a step that calculates, for each person, his or her associated sampling weight. This weight appears on the microdata file, and must be used to derive meaningful estimates from the survey. For example, if the number of individuals who smoke daily is to be estimated, it is done by selecting the records referring to those individuals in the sample having that characteristic and summing the weights entered on those records.”
For more detailed information on how the weights were calculated for the CCHS Cycle 2.1 survey please refer to the User’s Guide page 22.
How do we apply weights in SPSS?
[pic]
You will now have the option to select a variable to weight cases by. In our dataset the variable is called WTSC_M (master weights).
[pic]
Once you click OK and apply the weights - a box appears at the bottom righthand corner of the DataView screen that states the weight is on.
[pic]
Once the weight has been applied all results will reflect population results and NOT sample results.
Frequencies
All statistical analyses can be found under the Analysis menubar. For frequencies – Descriptive Statistics -> Frequencies
[pic]
To perform any analysis in SPSS – you wil be presented with a dialogue box which you fill in. There will also be several options boxes to explore.
[pic]
One neat trick to remember – in the dialogue boxes if you are uncertain of any tests or definitions – right-click on the test and you will be presented with a definition.
[pic]
To run the analysis – Click OK. The results are presented in a second window called the SPSS Viewer.
[pic]
Another neat trick to remember – if you can’t remember what the tables are showing you. Right-click and select Results Coach. This will take you through a mini-tutorial of the results table you selected. Very handy for students!
Result tables from SPSS can be copied directly into Word, PowerPoint and Excel. To accomplish this – Select the table in question – Copy object – move into Word – Select Paste.
Please note that the above results represent sample results. Once the weight is applied the results are:
[pic]
Crosstabulations
To perform a crosstabulation in SPSS – we’re interested in seeing the relationship the gender and self-rated Health.
Analyze -> Descriptive Statistics -> Crosstabs
[pic]
Select Gender of Respondent as the Column Variable and gencdhdi (self-rated health) as the row variable.
[pic]
If you want to calculate a Chi-square statistic to see whether there is a significant relationship between the 2 variables – Go to Statistics and select Chi-square
The results are as follows:
[pic]
Remember to take advantage of the Right-Click feature if you are uncertain about what the results are telling you…
Please note that there is a syntax available for SPSS. If you are interested in the syntax – select Paste in any of the dialogue boxes to open the syntax window. But most students enjoy the user-friendliness.
2. STATA
Open the Stata program
[pic]
Looks a lot different than SPSS – almost scary-like – no user-friendly or friendly-feeling windows.
“Stata is, at its heart, a command-driven application” (Getting Started with Stata manual).
There are a number of windows available to Stata Users.
Results window – will show commands that we run and associated notes (running log file), and the resulting tables of any analysis that were requested.
Command window – this is where you enter the commands that you would like performed.
Review window – lists the commands that have already been run.
Variables window – lists the variables that are available in the current dataset.
Open cchs2003.csv in Stata
[pic]
Use the Browse button to navigate to the Desktop and select the cchs2003.csv file. Please change the files of type to CSV – to find the cchs2003.csv file
[pic]
Select Specify value delimiter and comma. We know this information so let’s use it.
Note how there’s a message in the Stata Results window but there’s no data. From the message we can see that there are 8 variables and 61604 observations.
[pic]
What happens if you receive a message in the results window that looks like this:
[pic]
Stata reads a dataset and stores it in RAM memory of your machine. By default when you open the Stata program it allocated 1.00 Mb RAM memory for your data. We need to increase this to read a larger datafile. The complete CCHS Cycle 2.1 dataset contains 134072 observations – yet the dataset I’m reading in here still only contains 8 variables.
First – determine how much RAM is available on your machine… Go to my Computer – Right click and select properties.
[pic]
On this particular laptop there is a total of 512 Mb of RAM. To increase the amount of RAM memory Stata can use from 1.00 Mb to 50 Mb…
First clear the dataset – type Clear
Then type: Set memory 50000
You should see:
[pic]
Then try to import the dataset again…
[pic]
How do we see the data?
Type browse in the Command Window OR select the [pic] icon.
[pic]
You can move around in this window – you can sort the file by any variable by selecting Sort.
If you do not like the order the data is presented you can change the order by selecting the >> button or the Summaries, Tables & Tests -> Tables -> One-way Tables
[pic]
Enter the variable in question in the Categorical Variable box.
[pic]
Please note there are a number of options you can investigate at this point.
When you click OK on the dialogue box – you see the same results and command used in the Results window.
[pic]
Let’s try it with the weight variable
Command:
tab geocgprv [aw=wtsc_m]
[pic]
Crosstabulations
To run a crosstab in Stata you use the same command, only you list the second variable. So if we are interested in examining the relationship between province and health improvement – more exercise, we would type the following command in the Command box:
. tab cihc_8a geocgprv
[pic]
With weights:
tab cihc_8a geocgprv [aw=wtsc_m]
[pic]
But what if you want to precentages and a Chi-square test to see if there’s a significant relationship between the two questions?
tab geocgprv cihc_8a, chi2 row
[pic]
Now we can see that row (independent variable in this example) percentages, as well as the Chi-square test results. Please note that both SPSS and Stata give you the same results.
To save output
There’s not a nice feature in Stata to save your results window. Many people simply highlight the table of interest, copy and paste it into Word. You may need to some reformatting once the table is in Word.
You can log all the information from the Results window into a Log document. To do this
File -> Log -> Begin
And save it to a file. The log file is formatted for Stata – if you want to place information into Word or elsewhere, you will need to copy and paste as above.
3. SAS
Open the SAS program
[pic]
SAS has a number of windows to work with – very simliar to Stata.
Editor – where you will write your code
Log – SAS maintains a log of all processes it has run – this is where you will find your errors.
Output – this is where your results will be displayed
Explorer tab – allows you to move inside and outside the SAS program
Results tab – table of contents of all result tables in the Output window
Open cchs2003.csv in SAS
Rather than writing a program to import the cchs2003.csv file – we’ll use the Import feature in SAS.
File -> Import Data
SAS, very much like SPSS will walk you through a wizard.
[pic]
Our file is a CSV file – select this option from the Standard Data Source box.
Browse to the desktop to select the cchs2003.csv file. Select the options tab to insure that the data will be read from the second row and the variable names will be created from the first row.
[pic]
SAS arranges its data in a library format. To compare this to “My Computer” on our computers – think of a folder as a filing cabinet drawer – this is referred to as a library in SAS. Each file in the folder or each folder in the cabinet drawer is a dataset.
SAS has 2 kinds of libraries – temporary and permanent libraries. Today we will be working with the temporary library called Work. This means that anything we create in SAS will be cleared out when the SAS program is shut down. Permanent libraries are used to create permanent SAS datasets.
On the Import Wizard dialogue box leave library as Work – under Member – type in cchs2003. This will name our SAS dataset cchs2003.
[pic]
The next step allows you to save the program generated by SAS to import the datafile.
[pic]
Click Finish and the dataset has been imported.
Please note that nothing appears to happen – other than some print in the log Window.
If you look at the bottom of the Log window – it shows this:
[pic]
The work.nepal dataset was successfully created. We can also see that there are 8 variables and 61604 observations. BUT – as with Stata – there is no data to be seen.
To view the data we can go to the Explorer tab on the left side.
Click on the [pic] icon and select the Work library or [pic]icon.
You should now see the contents of the Work library – our cchs2003 dataset.
[pic]
If you double-click on the Cchs2003 icon – SAS will open the ViewTable. You can now browse your data as we could in Stata.
Options on the top menubar include sorting, form view, column attributes, table attributes, edit and browse. As in Stata, when you are browsing the data you cannot make any changes to the data. To edit the data you must enter the Edit mode. To do this you can select the Edit Icon [pic] .
To exit the ViewTable mode – Click on the x at the top righthand corner of the window.
Sometimes looking at the data in this format is a bit cumbersome – another way to look at it is by using the Proc Print procedure.
How does SAS work?
There’s the Data step – used to get your data into SAS and used to manipulate the data. This is one of the strong points for SAS.
To analyze the data you use a series of Procedures or PROCs. If you want to do anything with your data – look at it or process it you will probably use a PROC.
PROC PRINT
To view the data in the Results window we can use a Proc Print.
In the editor type:
Proc print;
Run;
[pic]
To run the program click on the “Running man” OR go to Run -> Submit.
When the process is complete SAS will automatically present you with the Output window. You should ALWAYS look a the log window first. If there are no problems or errors then proceed to the Output window.
In this case we get an Output Window Full message. We’ve filled the Output window buffer with the 61604 observations. Options are listed below. For this case we will select the third option – C- Clear window without saving. Click OK
[pic]
A snippit from the log window showing that everything is fine…
[pic]
A snippit of the Output window..
[pic]
Proc Contents
In SPSS we could gather information about the variables from the VariableView, and in Stata we used the describe command. In SAS we will use the Proc Contents procedure.
In the editor type:
Proc contents;
Run;
[pic]
To run a selection of the code in the SAS editor – highlight the code as above and hit the “running dude”.
Check the log window first before browsing the Output window..
[pic]
The output window shows the following:
[pic]
The same basic information we saw in both SPSS and in Stata. Again – we are presented with variable names, types, lengths, positions, and formats. No labels are visible nor any value labels.
Variable Labels
To add labels to the variables in SAS – since we are looking to make changes to the dataset we need to work in the Data step.
First we need to start with a Data statement. By doing this we are telling SAS that we are creating a new SAS dataset and we’re giving it a new name. Very similar to using a Save As… function in any other program.
Second, we need to tell SAS what dataset we’re are working with. We do this by using a Set statement.
Data cchs_lab;
Set cchs2003;
Now we can start adding our labels.
Label geocgprv = “Province”;
Label dhhc_sex = “Gender”;
Label dhhcgms = “Marital Status”;
OR
Label
geocgprv = “Province”
dhhc_sex = “Gender”
dhhcgms = “Marital Status”;
Either way works.
Once you have finished entering your labels – you need to complete the Data step with a Run; at the end.
So the program looks like this:
[pic]
Please note that certain parts of the program are a certain colour. This is really handy for debugging.
Once you submit this program – check you log window for any errors. If everything works out you should see:
[pic]
Try running the Proc Contents again to see whether the labels took effect or not.
[pic]
Value Labels
Same two-step process as was required for Stata. First we need to create the value labels (in SAS this is referred to as a format), then we need to apply the labels or the format.
To create the format we need to use a Proc format.
[pic]
Try adding this to the editor window. Run the code and check your log window for any problems.
This is what my log window looks like. So now we have a format for prov, sex, scale, and yesno.
[pic]
Now we need to apply these to the variables. Again we’re looking at changing the data so this will be conducted in a Data step. Let’s use the Data step we have already created.
At the bottom – before the Run statement let’s add the following code:
Format geocgprv prov.
Format dhhc_sex sex.;
Format dhhcmgs marstat.;
Run;
OR
[pic]
Try running the Data step again – check the Log window. If all looks well – run the Proc Contents to see what has changed…
Here’s my log window after I’ve added the format to my Data step…
[pic]
After I run Proc contents :
[pic]
So now we have the variable and value labels added to the SAS dataset.
Frequencies
SAS does have a Graphical User Interface (GUI) Analyst and/or the Enterprise Guide – but one of the strengths of SAS is the ability to write the code and add any options available. So we will be concentrating on the code to generate frequency counts.
To generate frequencies in SAS you need to use a procedure called Proc Freq. Let’s generate frequencies for geocgprv as we have for SPSS and Stata. To do this you need to use the following code:
Proc freq;
Tables geocgprv;
Run;
In the output window – after you’ve checked your log window you should see..
[pic]
Please note that the variable and value labels are shown.
Applying survey weights.
SAS acts very much like Stata when it comes to the weights. There is no weight on or weight off function. You must apply the weights for each procedure separately.
proc freq;
tables geocgprv;
weight wtsc_m;
Run;
The output window or results:
[pic]
Crosstabulations
To examine the relationship between province and how individuals self rated their mental health – we would look at generating a crosstab table. In SAS we use the Proc freq; to do this. We need to add the variables and place an ‘*’ between the two.
Your code should look like this:
Proc freq;
Tables gencdmhi*geocgprv;
Run;
When you run this and after you check your log window – the results should look like this:
[pic]
By default we already see the row and column percentages – but what if we want to see the Chi-square test results?
We need to add that option to our Proc freq code..
proc freq;
tables gencdmhi*geocgprv/chisq;
Run;
By placing a / after the tables and adding the chisq option – we see the following output:
[pic]
[pic]
To apply the weights – add the weight statement:
proc freq;
tables gencdmhi*geocgprv/chisq;
weight wtsc_m;
Run;
Output results:
[pic]
[pic]
If you check these results against SPSS and Stata – you will see that they match. However, with SAS you get a bit more information than in both SPSS and Stata.
To save output
The output window results can be copied and pasted into a Word document very much like with Stata. Formatting is maintained across the program. However, SAS has the ODS (Output Delivery System) which allows the user to create the output in another format – html, rtf, pdf, are examples. To create an HTML document from the Proc freq code we have – we need to add the following lines:
ods html file ="C:\test.html";
Proc freq;
tables gencdmhi*geocgprv/chisq;
Run;
ods html close;
These lines of coding will produce an html document called test.html and located in my root C:\ directory.
Here’s the log window results…
[pic]
If the SAS viewer has been installed on your system you will be able to preview your document…
[pic]
This document can now be posted on the web or sent easily from one person to another.
Overview
Each program has its merits and demerits. The choice of package will depend on a number of factors:
1 – choice of learning curve
2 – Time limitations – thesis due in a week vs. thesis due in a year
3 – type of analysis – social science researchers tend towards SPSS
- epidemiologists tend towards Stata
- animal scientists, mathematicians tend towards SAS
4 – access to support and help
More information:
SPSS. 2003. SPSS Base 12.0 User’s Guide. SPSS Inc., Chicago, Illinois
SAS. 2003. SAS Online Documentation. SAS Institute Inc., Cary, NC
Stata Corporation. 2003. Getting started with Stata for windows. A Stata Press Publication, College Station, Texas.
Your local Statistical Computing Consultant
Local SPSS, Stata, and SAS course and/or workshops
Quick comparison guide:
| |SPSS |Stata |SAS |
|Importing Files |Wizard |Menubar |Wizard |
|Variable Labels |VariableView |Label var “label” |In Data Step add |
| |Type in Label column | |Label var=”label”; |
|Value Label |VariableView |1 – create value |1 – create format |
| |Add in Values Column – dialogue box |label define gender 0 “male” 1 “female” |Proc format; |
| | | |Value gender |
| | |2 – apply value |0=”male” |
| | |label values bagndr1 gender |1=“female”; |
| | | |run; |
| | | | |
| | | |2 – apply formats in Data step |
| | | |format bagndr1 gender.; |
|Frequencies |Analyze |Tabulate variable |Proc freq; |
| |Descriptive Statistics | |Tables variable; |
| |Frequencies | |Run; |
| |Fill in dialogue box | | |
|Crosstabulations |Analyze |Tabulate variable1 variable2 |Proc freq; |
| |Descriptive Statistics | |Tables variable1*variable2; |
| |Crosstabs | |Run; |
| |Fill in dialogue box | | |
|Chi-square |Check option in Statistics box in Crosstabs |Tabulate variable1 variable2, row col chi2 |Proc freq; |
| | | |Tables variable1*variable2/chisq; |
| | | |Run; |
|Saving output |Select table |Highlight table |Highlight table |
| |Copy |Copy |Copy |
| |Paste into Word |Paste |Paste |
| | | |ODS |
|User-friendliness |High |Medium |Low |
|Learning Curve |Low |Medium |High |
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related searches
- interpreting spss correlation output
- sas convert character date to sas date
- stata string length
- difference in difference stata code
- stata generate id
- stata generate group id
- stata regression residual
- spss statistical test
- stata egen sum
- stata bysort command
- spss correlation output
- spss correlation analysis interpretation