Chapter1

Document Sample
Chapter1 Powered By Docstoc
					Introduction to SAS   1.0
Chapter 1
Introduction to the SAS system

1.1 What is SAS ?

1.2 What is a SAS program ?

A Sas program is a sequence of two kind of steps : DATA-steps and PROC-steps.



                                       A SAS program can be a DATA-step



Or a PROC step




                                      Or a combination of DATA and PROC steps




DATA step:
 Create SAS data sets.
 Can be used to generate reports


PROC step:
 Process and analyse SAS data sets and in many cases create new data sets.




Introduction to SAS                                                       1.1
Example of a simple SAS program :

Patient data are stored in a raw data file (ASCII) patient.asc. The program creates a SAS
data set, prints the data set with the patient numbers, the sex and the age, produces a
tabular report for sex and computes the mean of age.


                       data patient ;
                         infile 'd:\cursus\patient.asc';
   DATA                  input nr 1-8 admdat $ 10-17
                         admkind 19
   step                        disdat $ 21- 28 diskind 30
                               sex $ 32 age 34-36 ;
                       run;



                      proc print data = patient ;
                          var nr sex age ;
   PROC                  run;
   steps
                         proc means data = patient;
                          var age ;
                         run;




Introduction to SAS                                                            1.2
1.3 Executing a SAS program


When executing a SAS program, 2 kind of results are generated.


                  SAS log             Displays information about the executing of your SAS
                                      program (errors and warnings)

                  output              Displays results from Proc steps



SAS log and SAS output for the example from section1.2. :


SAS log :

          53     data patient ;
          54       infile 'd:\Mcursus\sas\sasV8\patient.asc';
          55       input nr 1-8 admdat $ 10-17 admkind 19
          56             disdat $ 21- 28 diskind 30
          57             sex $ 32 age 34-36 ;
          58     run;

          NOTE: The infile 'd:\Mcursus\sas\sasV8\patient.asc' is:
                File Name=d:\Mcursus\sas\sasV8\patient.asc,
                RECFM=V,LRECL=256

          NOTE: 10 records were read from the infile 'd:\Mcursus\sas\sasV8\patient.asc'.
                The minimum record length was 36.
                The maximum record length was 36.
          NOTE: The data set WORK.PATIENT has 10 observations and 7 variables.
          NOTE: DATA statement used:
                real time            0.05 seconds


          59
          60     proc print data = patient ;
          61      var nr sex age ;
          62     run;

          NOTE: There were 10 observations read from the dataset WORK.PATIENT.
          NOTE: PROCEDURE PRINT used:
                real time           0.00 seconds


          63
          64     proc means data = patient;
          65       var age ;
          66     run;

          NOTE: There were 10 observations read from the dataset WORK.PATIENT.
          NOTE: PROCEDURE MEANS used:
                real time           0.00 seconds



SAS output :




Introduction to SAS                                                                        1.3
Introduction to SAS   1.4
                                                                  Toolbar




                                                                  Log

      Explorer



                                                                  Output
                                                                  (behind
                                                                  Editor en
                                                                  Log)




Results                                                           Editor
(behind
Explorer)




            
            Example : Executing a SAS program

            Starting SAS

                      1. Double click on the SAS icon .




                      The following windows are available




            Introduction to SAS                             1.5
Opening a SAS program and executing a program :

          1. Select File  Open or click on the „Open‟-button     from the toolbar to open
          a SAS program .


          2. Select the desired program, click and select Open.




          The program is opened in the Editor window :

The Editor window :

              To open existing SAS programs and to edit them
              To write new programs
              To execute programs
              To save programs in a file




Introduction to SAS                                                            1.6
This SAS program has 1 DATA step and 2 PROC steps.

                The Editor window (enhanced editor) is specific for windows If you use
                      another operating system (UNIX, mainframe MVS) then programs are
                      opened in the Program Editor instead of in the Editor window.

                Under Windows, the Enhanced Editor and the Program Editor are
                      available. The Enhanced Editor is the default. There are a few differences
                      between both windows




          3. Click on the submit-button         to submit the program of select Run  Submit




Introduction to SAS                                                                   1.7
Looking at results

The Output window

            Is one of the primary windows and is open by default.
            Is activated when output is generated
            Cumulates all results from previous PROC steps.
          Using Edit  Clear All you can clear the output from that window




          Select the PROC PRINT output in the Results window.

          This window has the possibility to look at individual output of a PROC step, to
          store individual output and to save it.




          If you double click on the „Data Set WORK.PATIENT‟ in the „Print‟-map then the
          output from the PROC PRINT is showed in the „Output‟ window.

          If you use the right mouse, then a pop up menu is coming so that you can open,
          save or rename the output.




                                                                            pop up menu




Introduction to SAS                                                                1.8
          4. If you want to open the LOG window, select the LOG button




                                                                           SAS toolbar




Introduction to SAS                                                      1.9
1.4 Rules for writing a SAS program

Syntax rules

Each step in a SAS program is made up of one or more Statements

              data patient2;
                set patient ;
              run;
              proc print data = patient2 ;
              run;




A DATA step begins with the keyword DATA.
A PROC step begins with the keyword PROC.
The previous program exists of  DATA statement,  a SET statement,  a RUN
statement,  a PROC statement and  again a RUN statement.


A statement has two important characteristics
 It begins with a SAS keyword
 It ends with a ;

Form of the statements
 Can be in uppercase or lower case
 One statement can continue over several lines

Blanks separate « words » .


Note : a good programming style is to begin DATA, PROC and RUN statements in
column 1 and indent other statements.




Introduction to SAS                                                      1.10
1.5 SAS Datasets

Consider the following example:
                        Create a dataset PATIENT2


                                                    Read in a dataset PATIENT


                 data patient2;
                    set patient;
                 run;

                 proc print data = patient2;
                 run;



                                                                                Print a dataset PATIENT2 af




A SAS data set has two components: a data part and a description part.




The Data part

Data is arranged in a rectangular table

Rows = observations
Columns = variables




Introduction to SAS                                                                    1.11
There are two kinds of variables: character and numeric.
Character variables (SEX) have a maximal length of 32767 characters
Numerical variables (AGE, NR) have a default length of 8 bytes.



Missing values
Each variable has a value for each observation, possibly a missing value.

                       missing values




With character variables, a missing value is represented by a blank.
With numerical variables, a missing value is represented by a dot.




                In Windows version SAS version 8 , the following terminology will be used:

                      SAS data set              SAS Table



                      Variable                  Column


                      Observation               Row




Introduction to SAS                                                              1.12
The descriptor part

You can ask for description by Proc Contents data =…..The descriptor part of a SAS data
set contains:


-    general information on the data set :name , number of observations, date/time
     created, ….




-    Information for each variable : name, length, type, format, informat, …




SAS data set names and variable names


    Can be up to 32 characters long
    Can use uppercase or lowercase (or a combination of both) characters
    Start with a letter (a-z) or an _ (underscore)
     next characters may be : characters, numbers




Introduction to SAS                                                            1.13
Showing the descriptor part of a SAS data set

An overview of the SAS data sets is shown in the Explorer window. This is the fifth
default window which is opened when you start SAS.

Click on the Explorer tab




          If you want to activate the tree structure, select
                  View  Show Tree .




         Example PATIENT data set


          1. Double click on Work




          2. Select the table PATIENT. Use the right mouse to open the pop-up menu.




Introduction to SAS                                                            1.14
          3. Select Open. The VIEWTABLE window is opened .




          You can use the VIEWTABLE window to edit data, to sort or to delete
          observations or variables.


          4. Close the window and select Properties from the pop-up menu to see the
          general properties of the data set.




Introduction to SAS                                                             1.15
          Select Columns from the drop down list at the top of the window to see the list of
          variables .




Introduction to SAS                                                               1.16
1.6 SAS Data Libraries



What is a SAS data library?



SAS data sets are stored in SAS libraries.

A SAS data library is a collection of SAS files (data sets and SAS catalogs)


When starting SAS, 3 libraries are automatically activated.
Click double on Libraries in the Explorer window to show the libraries.




The WORK library



The WORK-library is a work library. Sas files are stored temporary in the WORK library.
When closing SAS, all the files are automatically deleted.




Introduction to SAS                                                            1.17

          Example:




In the log the reference to the work library is made explicit:




Each SAS file can be written with a two-level name:


                      libref.filename


    The first name (libref) is associated with a storage location (library)
    The second name refers to a specific name of the SAS file.


The name WORK.PATIENT refers to a data set PATIENT in the library WORK.

To refer to the data set PATIENT in the library WORK, you can use the double name
WORK.PATIENT or short PATIENT.
If there is no libref before the filename, then SAS assumes that it has as libref WORK.




Introduction to SAS                                                             1.18
The SASHELP and de SASUSER libraries




These libraries are permanent libraries.

    As a normally SAS user, you do not add files to the library SASHELP.
    The SASUSER library contains settings of the SAS system. You normally do not add
     files to this library..

The SASHELP and SASUSER library are permanent and files stored there can be used
in a later session again.

In order to know where the files from e.g. the SASUSER library are stored, we have to
know to which directory the library SASUSER refers.
Ask in the EXPLORER window View  Details




This depends on where on your PC the SAS software is installed.




Introduction to SAS                                                           1.19
         Example:

          Make a SAS data set DISTANCE in the SASUSER library.




          In the Log:




If you now double click on the library SASUSER, then the data set DISTANCE appears in
the list of data sets in this library.




Introduction to SAS                                                        1.20
Define a new library

It is more efficient to create your own libraries according to e.g. the projects where you
are working on. You can define a permanent library as follows:

Use the explore window





          For PC, windows:

          Assign a reference name (libref) CURSUS to the directory
          e:\data\kursussen\sas\sasversie8

          1. Click in the Explorer window on Libraries with the right mouse and select New .



          2. Type in the Name-field: the reference name of the library, here CURSUS.

          Engine = default.

          If you click “Enable at start-up” then this libref will be automatically assigned to
          this directory when SAS is starting.

          3. Click on Browse to select the directory, which will be used to store the SAS
          files of the CURSUS library. Click on OK.




          4. Click on OK




Introduction to SAS                                                                    1.21
          CURSUS is now available in the list of Active Libraries in the Explorer window.




Introduction to SAS                                                               1.22
Working with libraries

Copy a SAS data set

From the Explorer window, you can easily copy a SAS data set from one library to
another library.
(Use the tree layout)




          Example

          Copy the DISTANCE (from SASUSER) and the PATIENT data set (from the
          WORK) to the CURSUS library.




Introduction to SAS                                                          1.23
Refer to SAS data sets

Permanent SAS data sets

The data set CURSUS.PATIENT is a permanent data set since the libref is CURSUS and
not WORK.



To refer to a permanent data set, a two-level name need to be used.

                                     libref.filename
                      Permanent
                      SAS datasets



                                     CURSUS.PATIENT


Temporary data sets

Temporary data sets always have libref WORK. A two-level name work.test refers to a
data set TEST that is stored in the library WORK.



                                     libref.filename

                      Temporary
                      SAS datasets



                                      WORK.TEST




If you don‟t use a two-level name, then SAS assumes that you refer to the WORK library.


                                      libref.filename

                      Temporary
                      SAS datasets



                                                TEST




Introduction to SAS                                                          1.24

          Example



          Create a data set Patient_under30 in the library CURSUS that contains the
          patients with age < 30 years. .

          data cursus.patient_under30 ;
            set patient;
            if age < 30 ;
          run;


          Log:




Direct refer to a permanent data set


From version SAS 8 on, you can make references to the path and the SAS name itself as
follows:




          Example :

          Create a data set TEST in the directory d:\user which contains only 1 variable and
          1 observation (x = 100.)

          SAS code:

                      data 'd:\user\test' ;
                        x = 100 ;
                      run;


          SAS log :

                      127   data 'd:\user\test' ;



Introduction to SAS                                                               1.25
                      128     x = 100 ;
                      129   run;

                      NOTE: The data set d:\user\test has 1 observations and 1 variables.
                      NOTE: DATA statement used:
                            real time            0.04 seconds



          Remark that SAS has made a reference name for the library d:\user. When you
          double click on this library, the data set TEST appears.




Summary:

Data statement                     Libref             Member Name             Type
data total;                        WORK               total                   temporary
data „d:\user\total‟               Created            total                   permanent
                                   automatically
data mine.total                    mine               total                   permanent




Introduction to SAS                                                                         1.26
1.7 Editing and correcting SAS programs

Submitting a program with syntax errors

Have a look at the Log window to correct the errors.


         Example




           :

          Correct DAAT as DATA and LEEFTIJD as AGE and submit the program
          again.




Introduction to SAS                                                  1.27
Recalling a program

If you work with the Enhanced Editor, then the program stays in the Enhanced Editor
when you are working and submitting the program.
U can make corrections or enhancements and submitting the program again.


If you use the Program Editor (in UNIX or mainframe) then the program disappears when
the program is submitted. You can bring it back by recalling the program. (Use the Recall
command).




Saving a SAS program



A SAS program can be saved in an external file from the Editor window.




Use File  Save or the      button from the toolbar to save the file to an external file.
Use File  Save as if you want to specify the name and the directory.




Introduction to SAS                                                              1.28
It is common use the give SAS programs the extension .SAS




Introduction to SAS                                         1.29
1.8 Listing/HTML output

Listing output

When you start SAS, listing output is the standard output. The output is showed in the
output window in ascii test.




Introduction to SAS                                                            1.30
HTML output


Use Tools  Options  Preferences


Select the tab Results




Standard, the box to create listing output is crossed. If you want to produce both kinds of
output, you have to cross both boxes. If you only want to have html output, then crosses
only the box „Create HTML‟. The output is then not shown in the output window but in the
Results Viewer window.

Example of HTML output:




Introduction to SAS                                                              1.31

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:3
posted:8/6/2011
language:English
pages:32