chapter 1: introduction to sas sas programs: a sequence of statements in a particular order rules...

23
Chapter 1: Introduction Chapter 1: Introduction to SAS to SAS SAS programs: A sequence of SAS programs: A sequence of statements statements in a particular order in a particular order Rules for SAS statements: Rules for SAS statements: Every SAS statement ends in a Every SAS statement ends in a semicolon semicolon Upper/lower case does not matter in Upper/lower case does not matter in SAS SAS 1 © © Fall 2011 Fall 2011 John Grego and the University of South John Grego and the University of South Carolina Carolina

Upload: dorthy-boone

Post on 02-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Chapter 1: Introduction to Chapter 1: Introduction to SASSAS

SAS programs: A sequence of SAS programs: A sequence of statementsstatements in a particular order in a particular order

Rules for SAS statements:Rules for SAS statements:– Every SAS statement ends in a Every SAS statement ends in a

semicolonsemicolon– Upper/lower case does not matter in SASUpper/lower case does not matter in SAS

11

© © Fall 2011Fall 2011 John Grego and the University of South CarolinaJohn Grego and the University of South Carolina

Page 2: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Chapter 1: Introduction to Chapter 1: Introduction to SASSAS

Rules for SAS statements:Rules for SAS statements:– Statements can continue on next lineStatements can continue on next line– Statements can be on same line as Statements can be on same line as

other statementsother statements– Statements can start in any columnStatements can start in any column

22

© © Fall 2011Fall 2011 John Grego and the University of South CarolinaJohn Grego and the University of South Carolina

Page 3: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Chapter 1: Introduction to Chapter 1: Introduction to SASSAS

Rules for SAS Rules for SAS comments:comments:

Comment “boxes” Comment “boxes” are popularare popular

Comments: Two Comments: Two possible stylespossible styles

*….; *….; oror /*….*/ /*….*/

*Here is a *Here is a comment;comment;

/* Here is another /* Here is another comment */comment */

33

© © Fall 2011Fall 2011 John Grego and the University of South CarolinaJohn Grego and the University of South Carolina

Page 4: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS Data SetsSAS Data Sets

Specially formatted “worksheet”; Specially formatted “worksheet”; current extension is .sas7bdatcurrent extension is .sas7bdat

Variables represented by Variables represented by ColumnsColumns Observations represented by Observations represented by RowsRows Two Data Types; Two Data Types; Numeric Numeric andand

CharacterCharacter Default variable length is 8Default variable length is 8

44

Page 5: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS Data SetsSAS Data Sets

Choose data type based on how you Choose data type based on how you useuse the variable the variable

Example:Example: SSN could be characterSSN could be character Missing data in SAS is denoted by a Missing data in SAS is denoted by a

period (.) for numeric data and a null period (.) for numeric data and a null space for character dataspace for character data

Variable attributes (Type, Length, etc) Variable attributes (Type, Length, etc) are stored with the SAS data setare stored with the SAS data set

55

Page 6: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Rules for Naming SAS Rules for Naming SAS variablesvariables

Names can be 32 characters or fewerNames can be 32 characters or fewer Names must begin with a letter or Names must begin with a letter or

underscore (_)underscore (_) Names may contain Names may contain onlyonly letters, letters,

number, and underscoresnumber, and underscores Names can contain upper/lower case Names can contain upper/lower case

letters letters

66

Page 7: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Two Parts to SAS ProgramsTwo Parts to SAS Programs

DATA step:DATA step: When ads ask for “SAS experience”, When ads ask for “SAS experience”,

they mean experience with the they mean experience with the DATA stepDATA step

Begins with DATA statementsBegins with DATA statements Reads in and modifies dataReads in and modifies data Creates SAS data setCreates SAS data set

77

Page 8: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Two Parts to SAS ProgramsTwo Parts to SAS Programs

PROC step:PROC step: Begins with PROC statementsBegins with PROC statements Performs (statistical) analyses on Performs (statistical) analyses on

datadata Produces results/outputsProduces results/outputs

88

Page 9: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Two Parts to SAS ProgramsTwo Parts to SAS Programs

StepsSteps may contain may contain many many statementsstatements

Steps usually end Steps usually end when:when:1.1. Another step Another step

beginsbegins

2.2. A A RUN;RUN; statement statement appearsappears

DATA height;DATA height;

..SAS statements....SAS statements..

run;run;

PROC PRINT PROC PRINT DATA=height;DATA=height;

run;run;

PROC REG;PROC REG;

..SAS statements....SAS statements..

run;run;

99

Page 10: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Two Parts to SAS ProgramsTwo Parts to SAS Programs

SAS reads data sets SAS reads data sets one one observation at a timeobservation at a time-implicit -implicit loopinglooping

SAS executes steps line by lineSAS executes steps line by line Be sure to enter statements in Be sure to enter statements in

correct ordercorrect order

1010

Page 11: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Windowing EnvironmentWindowing Environment

PCs in labs and classrooms use PCs in labs and classrooms use windowingwindowing environment. environment.

See Sec 1.5 for information on other See Sec 1.5 for information on other SAS environmentsSAS environments

1111

Page 12: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS WindowsSAS Windows

3 windows in one frame and 2 3 windows in one frame and 2 windows in anotherwindows in another

““Editor”: Type in and edit SAS Editor”: Type in and edit SAS programs in the editor window (color-programs in the editor window (color-coded)coded)

““Log”: Contains notes about SAS code Log”: Contains notes about SAS code execution, SAS data sets, and execution, SAS data sets, and errors/warningserrors/warnings

““Output”: Printable resultsOutput”: Printable results1212

Page 13: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS WindowsSAS Windows

3 windows in one frame and 2 3 windows in one frame and 2 windows in anotherwindows in another

““Results”: Table of contents for Results”: Table of contents for Output windowOutput window

““Explorer”: Icons for file folders, SAS Explorer”: Icons for file folders, SAS libraries, and SAS data setslibraries, and SAS data sets

Additional output windows (HTML Additional output windows (HTML format, Graphics)format, Graphics)

1313

Page 14: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Windowing EnvironmentWindowing Environment

Once program is entered into Once program is entered into “Editor”, save it, then choose “Editor”, save it, then choose “Submit” under “Run” menu (Or “Submit” under “Run” menu (Or “make the little man run”)“make the little man run”)

Go to Output and Log windows for Go to Output and Log windows for results or notesresults or notes

If program disappears, choose “Recall If program disappears, choose “Recall Last Submit” under “Run” menuLast Submit” under “Run” menu

1414

Page 15: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Windowing EnvironmentWindowing Environment

In In anyany window: Choose “Edit”- window: Choose “Edit”->”Clear All” by right-clicking to clear >”Clear All” by right-clicking to clear window (Output and Log windows window (Output and Log windows can get cluttered)can get cluttered)

Log window -> Error messages, Log window -> Error messages, number of observations and number of observations and variables created. Often hard to variables created. Often hard to interpret, but into on intermediate interpret, but into on intermediate datasets can be helpfuldatasets can be helpful

1515

Page 16: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Windowing EnvironmentWindowing Environment

Printing/Saving Output:Printing/Saving Output: Directly from Output window (too Directly from Output window (too

many page breaks—wastes paper)many page breaks—wastes paper) Using Results window (can print/save Using Results window (can print/save

partial output—right click; select partial output—right click; select “Open in New Window”)“Open in New Window”)

Copy to file (Word or Notepad); edit Copy to file (Word or Notepad); edit and print.and print.

1616

Page 17: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Windowing EnvironmentWindowing Environment

Creating HTML Output:Creating HTML Output: Tools->Options/PreferencesTools->Options/Preferences Click “Create HTML”Click “Create HTML” Both styles of output will be availableBoth styles of output will be available

1717

Page 18: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS LibrariesSAS Libraries

Location where SAS data sets and Location where SAS data sets and SAS files are storedSAS files are stored

Often, an ordinary directory that Often, an ordinary directory that contains non-SAS files as wellcontains non-SAS files as well

Built-in libraries: Built-in libraries: Sashelp, Sasuser, Sashelp, Sasuser, Work Work (default library)(default library)

1818

Page 19: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS LibrariesSAS Libraries

Creating Libraries: Creating Libraries: In active Libraries windows, select File-In active Libraries windows, select File-

>New, or right click and select New>New, or right click and select New Use method in Section 1.11, or select the Use method in Section 1.11, or select the

sparkly file cabinet drawersparkly file cabinet drawer

Name:Name: Libref (8 characters or fewer)Libref (8 characters or fewer) Path (location for data sets to be stored)Path (location for data sets to be stored) Enable at start-upEnable at start-up

1919

Page 20: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS LibrariesSAS Libraries

Library location can easily be Library location can easily be forgotten (if not enabled)forgotten (if not enabled)

SAS prompts you to save in a separate SAS prompts you to save in a separate file the code that created the libraryfile the code that created the library

It’s convenient to include simple code It’s convenient to include simple code at the top of your file:at the top of your file:

libname mitchell ‘z:\stat 540\libname mitchell ‘z:\stat 540\Mitchell’;Mitchell’;

2020

Page 21: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS LibrariesSAS Libraries

Be careful that you do not write over a Be careful that you do not write over a saved file:saved file:

/* This code stores a data set from WORK in /* This code stores a data set from WORK in MITCHELL */MITCHELL */

/* It can also accidentally overwrite any /* It can also accidentally overwrite any changes in mitchell.anpp in subsequent changes in mitchell.anpp in subsequent runs */runs */

libname mitchell ‘z:\stat 540\mitchell’;libname mitchell ‘z:\stat 540\mitchell’;

data mitchell.ANPP; set ANPP;data mitchell.ANPP; set ANPP;

2121

Page 22: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

Viewing Data Sets in Viewing Data Sets in ExplorerExplorer

Double-click on a library, then double-click Double-click on a library, then double-click on an available data set (e.g, “Class” in on an available data set (e.g, “Class” in Sashelp) or right-click and select Properties.Sashelp) or right-click and select Properties.

Close window when finished examining data Close window when finished examining data setset

Examine data set worksheet for problemsExamine data set worksheet for problems Right-click on column label and select Right-click on column label and select

“Column attributes” to obtain info on the “Column attributes” to obtain info on the variable in the data set.variable in the data set.

2222

Page 23: Chapter 1: Introduction to SAS  SAS programs: A sequence of statements in a particular order  Rules for SAS statements: –Every SAS statement ends in

SAS System OptionsSAS System Options

OPTIONSOPTIONS statement can appear statement can appear before the before the DATADATA statement statement

Frequently used to improve Frequently used to improve pagination and on-screen readabililty pagination and on-screen readabililty of outputof output

OPTIONS LINESIZE=80 NOCENTER NODATE OPTIONS LINESIZE=80 NOCENTER NODATE PAGESIZE=64 NONUMBER;PAGESIZE=64 NONUMBER;

2323