SAS for Windows

by Robert A. Yaffee
Statistics and Social Science Group
Academic Computing Services
Information Technology Services
Courant Institute of Mathematical Sciences
251 Mercer Street
New York, New York 10012


November 15, 1995
updated February 9, 2001

1. Accessing SAS/windows
a. Go to the computers
b. Turn on the computer
c. When "Novell Client" dialog box appears indicating that
usage is restricted to authorized NYU users, click on "OK"
d. Windows 95 is then invoked automatically
e. Several folders will appear on a green desktop

2. Invoking SAS Windows
a. Double click on "Applications" folder on Windows 95 desktop
b. Double click on "Statistical Analysis" folder in the new
array of folders that appears
c. Double click on "The SAS System" folder in the new
array of folder that appears
d. Double click on the "The SAS System for Windows v.612"
that appears
e. Click on preferences.
f. Be sure that the Menu popup option is selected (blackened) by clicking
on it.
g. Be sure that the Command box is selected.
h. Be sure that the tool bar is selected.
i. Insert Mode should have insert selected.
j. Click on the save radial button at the bottom.

3. Preparing a SAS Command file.
a. A SAS command file is a SAS program consisting of a sequence of commands, which tell
SAS how to define and read data from a file and what to do with these data.
b. A SAS program file (command file) may be created with your
favorite editor in standard ascii (text).
1. Ted, Kedit, Edlin, Edit, Brief, Emacs are some
of those editors which may be used for this purpose.
2. WordPerfect or Word may also be used as long as the
file is saved in standard ascii format: SAS will not read WordPerfect or MS Word format.
3. Alternatively, use the SAS program editor, itself, to
construct the command file.
c. A SAS command file ordinarily consists of a data step and a
statistical procedure step.
d. A simple example of a SAS command file, containing the data to
be used:
          Options ls=80 ps=60;
          Title  'Sample Program';
          
     Proc format;
           Value sx 1 = 'female'
                    2 =  'male';
          Value maj 1 = 'science'
                    2 =  'liberal arts'
                    3 =  'fine arts'
                    4 =  'business'
                    5 =  'pre-med'
                    6 =  'pre-law';
           Value agp 1 = 'minor'
                     2 = 'adult';

     Data One;
          Input id 1-2 age 4-5 sex 7 major 9 grade 11 pretest 13
               postest 15;
     
     Label id =  'Respondent id'
              Age = 'Age of Respondent in years'
              Sex =  'Gender of Respondent'
              Major = 'Field of Study'
              Grade =  'Student year'
              Pretest = 'Student pretest score'
              Postest = 'Student Post-test score';

     If sex = 1;
     Diff = Postest - Pretest;
     
     If age lt 21 then agegrp = 1;
      else agegrp = 2;

          format major maj.;
          format sex sx.;
          format agegrp agp.;

     Cards;
     01 23 1 1 4 2 9
     02 34 2 3 4 5 9
     03 35 1 2 4 3 8
     04 19 2 4 2 5 9
     05 20 1 4 3 4 8
     06 21 2 5 4 3 7
     Proc Print label;
          Title2 'Listing of Data';
      Run;
     Proc Freq; Tables sex major ;
          Title2 'Range Check: Percentage Tables of Sex & Major';
      Run;
     Proc univariate normal plot; var age;
          Title2 'Univariate Analysis of Age';
      Run;
     Proc Corr; Var age grade;
          title2 'Consistency check';
      Run;
4. Options statement.
a. In the options statement, there is a page size
and a line size subcommand. The PS sets the page size
(length) and LS sets the line size(width).
   Options ls=80 ps=60;

5. Program Title.
a. Give the program a title. A SAS program can have up
to 10 titles. The second title begins with a Title2
statement, while the third title begins witha Title3
statement, etc. The title text may be enclosed in single or
double quotes.
 Title 'Data Cleaning Preview';
title2 'Your Name, Your Class';
Title3 'Project Name';

6. File Definition: Using an external data file.
a. Don't use a card statement with your data following it.
b. Instead, insert an infile statement between the Data One and the
Input statement. This infile statement defines the path and data file name in which the data may be found.
c. For example
     Data One;
          infile  'c:\tmp\datafile.dat';
          Input id 1-2 age 4-5 sex 7 major 9 grade 11 pretest 13 
              postest 15;
d. The Dataset thereby created is called Data One. This dataset
may be used and transfered later with reference to the dataset
name, One.

7. Data Definition: Variable definition and data location.
a. If there is at least one blank space between the variables, then you
may use list input, where a list of variables in the order that the data are contained in the data file may be used.
b. For example:
    input id age major sex;
c. If the data are not separated by common delimiters, such as blanks,
then "column input" should be used. An example of column input, where the column range is specified after each variable in the input statement, is given in section 4c above.
d. Alphanumeric variables (string variables) may be used. When they
are they should be followed by a dollar sign in the input statement. For example,
		Input fname $ 1-10  age;
Here the first name, the variable name for which is fname, occupies columns 1 through 10, in the data file. Fname is an alphanumeric or string variable, the data for which are letters within the data file rather than numbers. It is suggested that alpha variables be avoided by beginning users, for numerous complications may follow from attempting to use them.

8. Labels.
a. Variable labels up to 40 letters long may be used to describe
the variables. See the example above.
b. Please note that the variable label command begins with
LABEL not Labels.

9. The "Subsetting If" statement.
a. When part of a dataset is to be subsetted out for analysis,
a subsetting if statement is employed. In the program above,
 
                  If sex = 1; 
means that only the females, the value of the
sex variable for which is 1, are used in the analysis on
these data.
b. It should be noted that logical or mathematical operators
can be used. For example, one could specify "ne" for
"not equal to." Hence the same statement could be
written as:
             if sex ne 2;

10. Recodes.
a. When the age variable is collapsed into a few categories,
an "If then, else" statement may be used. In the example
provided in the program above, the age variable is recoded
into two categories: minor and adult. The syntax for this recoding is given by:
         If age lt 21 then agegrp=1; 
                 else agegrp = 2;  
b. Other mathematical or logical operators can be used inter-
changeably here. The same "if then, else" statement could be
written as:
         if age < 21 then agegrp eq 1;
                 else agegrp = 2;
c. Other permissible logical (Boolean) operators include:
     gt  greater than
lt less than
ge greater than or equal to
le less than or equal to
ne not equal to
& and
| or
~ not

d. Mathematical operators include:
     =  equal to 
~= not equal to
> greater than
< less than
=> equal to or greater than
=< equal to or less than

11. Formats
a. Value labels may be formed as formats. They are listed in a
Proc format. An example of the Proc Format may be found in the program in the following type of syntax:
          Proc format;
          Value maj 1 = 'science'
                    2 = 'liberal arts'
                    3 = 'fine arts'
                    4 = 'business'
                    5 = 'pre-med'
                    6 = 'pre-law';
           Value sx 1 = 'female'
                   2 =  'male';        
b. Such label formats are attached to the variable by a format
statement preceding the procedures. The syntax of the format statement follows:
          format sex sx.;

12. Missing Values.
a. Missing values may be indicated by the use of the period. A
miscode may be converted to missing by the following statement:
If sex = 3 then sex = . ;

13. Assignment statements.
a. New variables may be constructed with assignment statements
b. Age may be constructed from birthyear in the following way:
Age = 1995 - birthyear ;
c. A Difference score is computed by subtracting the pre-test
from the Post-test student score:

Diff = Postest - Pretest;

14. Data Preview
a. Prior to running statistical procedures, be sure that SAS is
reading your data correctly. Run a Proc Print to be sure that the input format is properly specified and that the data is not being mistakenly truncated.
b. Perform a range check by executing a Proc Freq. If you
obtain a sex = 3, then that score is out of range and the miscode should be corrected.
c. Where possible, run a consistency check:
    Proc Corr;
if your variables are continuous. If they are ordinal
in nature, then one may use:
          
         Proc Corr Spearman;
         run;

15. Graphical Examination of Data helps analysis. Comments are enclosed by /* and */ .

           /* Discrete Variables can be examined with PROC CHART */

            PROC CHART;
               VBAR region/discrete;
               run;

            PROC CHART;
               HBAR sex/discrete;
               run;

         /* Continuous Variables can be plotted */

            PROC PLOT;
               Plot GDP*time;
                run;

            PROC TIMEPLOT;
               ID Month;
               PLOT GDP;
                run;

            symbol1 i=join v=star c=green;
            symbol2 i=join v=plus c=blue;
            PROC GPLOT;
              PLOT (GDP forecast) * time /overlay;
               run;
16. Statistical Procedures.
Statistical procedures follow the data step. Each Statistical procedure is followed by a "run" command.
Some commented (comments are enclosed by /* and */) basic examples include:
         /*  A simple Frequencies Analysis of variables w, x and y */

         PROC FREQ; Tables w x y;
          run;
         
         /*  alternatively this may be invoked by the following command */

         PROC FREQ; Tables w--y;
          run;
         
         /* A Crosstabulation of Discrete Variables x and y     */

         PROC FREQ; TABLES x*y/chisq;
          run;
          
         /* A Simple Means Analysis of variable z                */


         PROC MEANS; var age;
          run; 
  
         /* A summary univariate analysis of varable age          */

         PROC UNIVARIATE normal plot; var age;
          run;

         /*  An independent samples t-test of variable age by gender */

         PROC TTEST; 
            class sex;
            var age;
              run;



         /*  A Paired-samples t-test of variable Diff is conducted */

         PROC MEANS mean std stderr t prt;
             Var Diff;
              run; 


         /*  A Pearson correlation between variables x & y        */

         PROC CORR; var x y;
            run;

         /* An Alpha Reliability Analysis of variables VV through YYY */ 

         PROC CORR alpha; var VV--YYY;
            run;


         /* A One-Way Fixed Effects Means Model  */
         /* testing reading achievement by different teaching methods */
         /* including test for homogeneity of variance and contrasts  */
         /* between levels                                            */
 
            PROC GLM;
               class Method;
                model readach = method/noint e;
                means method/Hovtest=Levene(type=abs);
                estimate 'L1' Method 1 -.5 -.5;
                estimate 'L1 v L2' Method 1 -1 0;
                estimate 'L1 v L3' Method 1 0 -1;
               run;
               
                       or

            PROC MIXED Cl Covtest;
               class Method;
               Model Readach = Method/DDFM=Satterth;
               Repeated/GROUP=Method;
               LSMeans Method/PDif=all adjust=Sidak;
               run;

         
         /* If Methods are a random sample of methods, then a random */
         /* effects model can be used                                */


         PROC GLM;
               Class Method;
               Model Readach = Method/SS1 SS2 SS3 SS4 e1 e2;
               Random Method/Test;
               LSMeans Method/PDiff=all Stderr adjust=bon;
               run;


         /* A Two-Way Treatment Structure with Unequal Variances      */
         /* Completely Randomized Block Design                        */
         /* Source: "Examples for Analysis of Messy Data Mixed Models */
         /* (1999). George A. Milliken and Dallas E. Johnson          */
         /*  Dept of Statistics, Kansas State University, p.9         */

           PROC MIXED;
               Class Blk A B;
               Model Y = A A*B/DDFM = Satterth;
               Random Blk;
               LSMeans A|B/Diff;
               Repeated/Group=A*B;
             run;   


         /* A Two-Way Treatment Structure Mixed Effects Model         */
         /* A is a fixed effect and B is a random effect              */

             
        PROC GLM;
            Class A B;
            Model Y = A B A*B/SS1 SS2 SS3 SS4 e;
            Random B A*B/Test;
            LSMeans A|B/adjust=bon;
            run;



         /* A full-factorial fixed Effects ANOVA of Y by SES and REGION */

         PROC GLM;
            class SEStype region;
            model Y = SESTYPE|region/SS1 SS2 SS3 SS4;
            means region/ tukey;
            means region*SESTYPE/Scheffe;
            OUTPUT OUT=RESDATA R=RESID1 P=PRED1;
             run;

         /* Testing the Residuals for Homogeneity */

            PROC PLOT DATA=RESDATA;
             plot RESID1*PRED1;
             run;

         /* Testing the Residuals for Normality   */
       
            PROC UNIVARIATE DATA=RESDATA normal plot;
             Var resid1;
            run;


         /* If variances are found to be unequal, then   */
         /* PROC MIXED should be used                    */


         PROC MIXED Cl Covtest;
            class SEStype region;
            model Y = SESTYPE|region/DDFM=Satterth;
            repeated/Group=region*SESTYPE;     
             run;


         /*  An OLS Regression analysis of Y  on X1,X2, and X3   */
         /*  with tests of assumptions                          */
         
         PROC REG simple;
            model Y  = X1 X2 X3/ss1 ss2 corrb Stb Vif Collin Spec DW R;
            OUTPUT OUT=RESDAT R=RESID P=PRED STUDENT=RSTD;
               run;

         /* Testing residual assumptions of the regression */

         PROC PLOT DATA=RESDAT; 
            PLOT RSTD*PRED;
         Title 'Examining Homogeneity of Residuals';
            run;

         PROC UNIVARIATE normal plot; VAR RESID;
            run;

         /* Logistic Regression Analysis where dependent variable (DV) */
         /*  is binary or dichotomous in coding                        */
         /*  Independent variables should be dummy coded or continuous */

         PROC LOGISTIC Descending Ctable pprob=.5;
            Model DV = X1 X2 X3 X4/
                corrb aggregate scale=pearson lackfit rsquare iplots;
            run;



17. Job Submission and Retrieval.
a. Save your file before running it.
b. Click on the right metallic square at the upper left of the
program editor window.
c. A pop down menu will appear.
d. Click on menu.
e. Click on file.
f. Click on Save As.
1. Use Save As because SAS remembers the last name
used and employs that name unless you specify otherwise.
2. Once this is saved, click on locals the top of the page. A
pop down menu will appear.
3. Then click on submit.
4. This will submit the file you have in your editor for
processing. You will be able to observe the execution of the commands in your output window.

18. Diagnosing the command file for errors and warnings.
a. Before reviewing the output, go to the log window
b. Examine the file for errors or warnings.
1. Errors are fatal to completion of the program.
They will cause the program to terminate prior to completion. Such abnormal ends of jobs are figuratively called "crashes, abends, or bombs."
2. Warnings are not fatal and will allow the program to
complete, although warning notes are issued when and where they are appropriate.

19. Editing the Program.
a. Click on the Window option in the menu bar at the top; select
the Log Window.
b. Within the Log Window, take note of the errors and warnings
given to explain what was wrong above the underlined part of the command for that is where the error was first detected.
c. Go back up to the Window option, and select the
Program Editor Window.
d. Begin correcting errors at the top of the program and
move downward. One error may engender a chain of other errors, so correction of early errors may eliminate subsequent ones.
e. This procedure assures correction of error chains and
facilitates efficient debugging of the program.
f. When all of the errors and warnings have been corrected, the
program is ready to be submitted for execution.

20. Job Submission (Running the Program).
a. Click on the locals option in the menu bar at the
top of the SAS program manager.
b. Submit the corrected program file by clicking on "submit."

21. Job Retrieval.
a. The output appears in the Output Window.
b. The output may be reviewed by maximizing the Output
Window by clicking on the up arrow (wedge) in the upper right hand corner of the Output Window and scrolling downward or upward.

22. Printing the output.
a. Once the output is satisfactory, you may wish to print it.
b. Click on the metalic square in the upper left corner of the
Output window.
c. When the menu appears click on print.
d. Your output will be printed at the HP Laserject
network printer.

23. Coping output to from SAS/Windows to WordPerfect/Windows
Documents.
a. Minimize the SAS for windows by clicking on the down
arrow (wedge) in the upper right of the SAS Window.
b. Open a WordPerfect Window. Double click on the
WordPerfect 6.1 /Windows icon.
c. Open up up your document: Click on File and then in the
pop down menu, click on Open, and then insert the path and filename of the file you wish to open in the filename space. Minimize WordPerfect with the down arrow(wedge) in the far upper right of the WordPerfect for Windows program.
d. While in SAS, go to the output window or input
window.
e. Mark the text you wish copied.
f. Click on copy to paste buffer.
g. Minimize the SAS/Windows (click on down
arrow[wedge] at top right).
h. Maximize the WordPerfect Program
(Double click on minimized Word perfect icon).
i. Position the cursor where you would like the output to be
inserted.
g. Go to the format option in the upper menu, click on
margins: Set your margins to .250" on the left and on the right margin.
h. Then go up to the edit option in the menu bar
and click on edit.
i. Click on paste special and then paste as unformatted text.
j. Position your cursor after the inserted text.
k. Click on edit and then margins and reset the margins to
1" on each side or whatever margin size you wish.

24. Saving the Program, Log, and Output while in SAS/Windows
a. Select the window of your choice by clicking on the
Window option in the menu bar at the top of the SAS program.
b. Saving Progam files
1. Click on Window and select program editor.
2. Go to Locals and select recall text to bring back to
the program editor the text of the last program file that was run.
3. Click on the upper left metallic square at the top of
the program editor window.
4. A popdown menu will appear.
5. Click on file option in that menu.
6. Click on Save As option.
7. Click on Write to File option.
8. Specify a file name for the program file to be
saved.
9. Click on the OK radial button at the top right of the
Save As menu.
c. Saving Log Files.
1. If you have not finished debugging your file when
you have to temporarily put away your work, you may save the last log file, in which the error and warning diagnostics are listed.
2. Go to the Window option at the top of the menu,
click on Window, and select the Log Window.
3. Go to File option in menu bar at top of the Log
Window.
4. Click on Save As.
5. Click on Write to File.
6. A Save As Log menu will appear.
7. Enter a name for the Log file, such as Prog1.log
8. Click on the OK radial button at the top left of the
Save As Log Window.
d. Saving output
1. Click on file in the upper left of the menu bar
2. Click on save as
3. Click on write to file
4. Enter a file name, such as example.lst
5. Then click on OK to save your output file as
Example.lst.

25. Exiting the SAS/Windows system.
a. After saving your program, log, and output files, go to
the file option at the top left of the menu bar on the top of the SAS program manager.
b. Click on File.
c. Click on Exit SAS.
d. You are asked whether you really wish to terminate the
preliminary SAS session.
e. If so, click on OK.
f. This act ends your preliminary SAS session.
g. Now the analytical fun can begin!

This page has been accessed times. Counter courtesy of Web-Counter.


Home - last updated 10 February 2000