Tag Archive: SAS BASICS


Notes on SAS


 Here are just some basics notes on SAS.  This is a compilation and more will be added as time passes.  Some notes will be spun off as a page or blog entry.

 

The Variable’s Length Attribute

The type of data that a variable contains is only one of the attributes a variable can have.  The next attribute is the variable’s length. SAS assigns a length of 8 to all variables unless you specify otherwise.  When all the data for a variable does not fit the defaults, you have to give SAS special instructions.  To describe variances from SAS’s default assumptions, use an informat or a format.  An informat tells SAS how to look at the data as it reads it.   A format gives SAS special instructions about displaying the data.

 

Format vs. Informat

For example, you may want a number to be printed in a social security number format: nnn-nn-nnnn.   A format tells SAS the pattern to use when displaying the data value.  Or, as in the gasoline data, you have three separate data values that comprise a date.   An informat tells SAS the pattern to use when reading in the three date values.

The final SAS attribute that can be specified is a label that is printed instead of the variable name.  SAS procedures print the variable’s name on reports, charts or graphs. To give a more descriptive name,  you may specify a label.

 

Telling SAS where to get the data from (File-Handling Statements)

You tell SAS where to find the data with one of the file-handling statements below. These will be explained as you need to use them.  Examine the following table  Statement Data is found…

  1. SET In a SAS data set
  2. INFILE In an external file
  3. CARDS

Program Statements

After reading an observation, SAS executes each program statement found in the DATA step.  Program statements can include the following and many, many more:

IF-THEN/ELSE — to test conditions

DELETE/OUTPUT — to control writing to the data set being created

Once all the program statements are executed, unless SAS is told otherwise with a specific programstatement, the observation is written to the SAS data set named in the DATA statement.   Then the execution phase begins all over again. It continues to process observations until it looks at the input data and sees an end-of-file indicator.  

 

Basics of a SAS Program


 The following is a basic example of a SAS program.   The program goes through the following steps.

  1. Creates a Dataset
  2. Defines the columns (commonly referred to as variables in SAS)
  3. Reads inline values to populate the SAS dataset with
  4. Prints a basic report displaying all values in the dataset
  5. Prints output given the mean of 2 numeric variables specified

If you are new to SAS but familiar with SQL, then as you learn SAS it will be easier if you think of SAS Datasets as Tables and Variables as Columns.  

Below is the sample program.  Underneath that is the program with comments.  Either of these programs can be copied and pasted into SAS and run with no other modification.

Simple SAS program example

 

DATA EMP_DATA;

INPUT EID : $5. F_NAME $ L_NAME $ AGE YRS_EMP;

CARDS;

15486 Mike Peters 41 8

52527 Louis Henna 52 14

54946 Sam Bass 31 4

17854 Cesar Chavez 25 7

77747 Fyodor Roosevelt 30 5

;

PROC PRINT DATA=EMP_DATA;

PROC MEANS MEAN; VAR AGE YRS_EMP;

 

Simple SAS program example with comments

 

 

/* This statement will tells SAS to create a dataset named EMP_DATA */

DATA EMP_DATA;

 

/* This statement describes how the data will be organized in the dataset */

INPUT EID : $5. F_NAME $ L_NAME $ AGE YRS_EMP;

 

/* CARDS as opposed to INFILE gives SAS the values to populate the dataset with */

CARDS;

15486 Mike Peters 41 8

52527 Louis Henna 52 14

54946 Sam Bass 31 4

17854 Cesar Chavez 25 7

77747 Fyodor Roosevelt 30 5

;

 

/* PROC PRINT gives tell SAS to print a report showing all values in the dataset */

PROC PRINT DATA=EMP_DATA;

 

/* PROC MEANS, in this example gives us the mean AGE and YRS_EMP (Years Employed)  */

PROC MEANS MEAN; VAR AGE YRS_EMP;