eclt 5810 sas programming - introduction

32
ECLT 5810 SAS Programming - Introduction

Upload: others

Post on 19-Jun-2022

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ECLT 5810 SAS Programming - Introduction

ECLT 5810SAS Programming - Introduction

Page 2: ECLT 5810 SAS Programming - Introduction

Why SAS?

Able to process data set(s). Easy to handle multiple variables. Generate useful basic analysis

Summary statistics Graphs

Many companies and government agencies adopt it

2

Page 3: ECLT 5810 SAS Programming - Introduction

SAS Programs

3

SAS program goes here

Page 4: ECLT 5810 SAS Programming - Introduction

Composed of SAS Statements All SAS Statements end with a Semi-colon EXCEPT Data lines Example SAS Statement:

input name $ sex $ age height; Comment lines are preceded by *

4

SAS Programs

Page 5: ECLT 5810 SAS Programming - Introduction

<= 32 characters for SAS 9.0 or above <= 8 characters for SAS 8 or below Must begin with a letter

5

SAS Variables Names

Page 6: ECLT 5810 SAS Programming - Introduction

Beginning: Create a SAS data set Middle: Work with data using SAS procedures (PROCs) End: RUN the program

6

Composition of SAS Programs

Page 7: ECLT 5810 SAS Programming - Introduction

SAS is flexible. Can read data from many sources Sometimes you can get SAS data sets from data sources

(BLS, etc.) First step is to convert raw data to a SAS data set

7

SAS Data and Data Sets

Page 8: ECLT 5810 SAS Programming - Introduction

A SAS Program: Beginning

SAS Statements: data, input, datalines

data testdata1;input id height weight gender $ age;datalines;1 68 144 M 232 78 150 F 343 65 150 M 35

8

Page 9: ECLT 5810 SAS Programming - Introduction

Beginning: Data Step Processing

data:Tells SAS the name of the SAS data set beingcreated.

9

data testdata1;input id height weight gender $ age;datalines;1 68 144 M 232 78 150 F 343 65 150 M 35

Page 10: ECLT 5810 SAS Programming - Introduction

Beginning

input:Tells SAS the names of the variables being read. varname $ means character data.

10

data testdata1;input id height weight gender $ age;datalines;1 68 144 M 232 78 150 F 343 65 150 M 35

Page 11: ECLT 5810 SAS Programming - Introduction

Beginning

datalines/cards:Tells SAS the following lines are data. Data must follow

11

data testdata1;input id height weight gender $ age;datalines;1 68 144 M 232 78 150 F 343 65 150 M 35

Page 12: ECLT 5810 SAS Programming - Introduction

Delimiters

Must separate variables on cards or external files Accomplished with “delimiters” Spaces are common, SAS default Can also use other characters, but must tell SAS

12

Page 13: ECLT 5810 SAS Programming - Introduction

Field: Smallest unit of data. One observation of a variable. Can be either character (letters and numbers) or numeric (numbers only).

Record: A single line of input. Contains one or more fields File: A collection of records

Some Definitions

13

Page 14: ECLT 5810 SAS Programming - Introduction

input name $ sex $ age height weight;

alfred M 14 69 112alice F 13 56 84barbara F 14 62 102henry M 15 67 135john M 16 70 165sally F 16 63 120;

A Character Field

14

Page 15: ECLT 5810 SAS Programming - Introduction

input name $ sex $ age height weight;

alfred M 14 69 112alice F 13 56 84barbara F 14 62 102henry M 15 67 135john M 16 70 165sally F 16 63 120;

A Numeric Field

15

Page 16: ECLT 5810 SAS Programming - Introduction

input name $ sex $ age height weight;

alfred M 14 69 112alice F 13 56 84barbara F 14 62 102henry M 15 67 135john M 16 70 165sally F 16 63 120;

A Record

16

Page 17: ECLT 5810 SAS Programming - Introduction

input name $ sex $ age height weight;

alfred M 14 69 112alice F 13 56 84barbara F 14 62 102henry M 15 67 135john M 16 70 165sally F 16 63 120;

A File

17

Page 18: ECLT 5810 SAS Programming - Introduction

Input Styles: List Input

input x1 x2 m; * input the variables;

This statement reads in the data in a SAS program.

When only the variables are listed, with $ to indicatecharacter variables, it’s called “List Input”, the simplestinput style in SAS.

18

Page 19: ECLT 5810 SAS Programming - Introduction

Fields must be separated by at least 1 blank Each field must appear in order Missing values must be represented by a placeholder

( a period . in this case) No embedded blanks in character fields Data must be in a standard format (e.g. text file)

Rules for List Input

19

Page 20: ECLT 5810 SAS Programming - Introduction

Looking at Data in SAS

After creating a SAS data set, it’s a good idea to look at the data to make sure it was read correctly.

You can use proc print to write the data to the outputwindow, or you browse the data interactively.

20

Page 21: ECLT 5810 SAS Programming - Introduction

Middle: Work with data

procA SAS procedure. These are how you work with thedata in SAS. There are many SAS procedures.

printSAS procedure to create an output file. By default,uses the data from the last data statement.

proc print data=testdata1; title "A Data Set";

run;

21

Page 22: ECLT 5810 SAS Programming - Introduction

Run Programs

22

Page 23: ECLT 5810 SAS Programming - Introduction

Sample Output

23

Page 24: ECLT 5810 SAS Programming - Introduction

Data SelectionUse if command to select data

24

data testdata1;input id height weight gender $ age;if gender=‘M’;datalines;1 68 144 M 232 78 150 F 343 65 150 M 35

Page 25: ECLT 5810 SAS Programming - Introduction

Sample Output

25

Page 26: ECLT 5810 SAS Programming - Introduction

Summary Statistics in SAS

• Means and Standard Deviations can be easily calculated for variables in a SAS data set using the means procedure

• Format:proc means;

var v1 v2 v3;

• List all the variables you want summary statistics for on the second line

26

Page 27: ECLT 5810 SAS Programming - Introduction

Summary Statistics in SAS

proc means;var height weight;run;

27

Page 28: ECLT 5810 SAS Programming - Introduction

Output from proc means

28

Page 29: ECLT 5810 SAS Programming - Introduction

data htwt; input name $ sex $ age height weight; x = height + weight; y = age**2; z = 3*age - 5;

cards;alfred M 14 69 112alice F 13 56 84barbara F 14 62 102henry M 15 67 135john M 16 70 165sally F 16 63 120;

proc means;var x y z;title 'Summary Statistics';

proc print; title 'Height-Weight Example #1';

run;

A Sample Program

29

Page 30: ECLT 5810 SAS Programming - Introduction

The Output

30

Page 31: ECLT 5810 SAS Programming - Introduction

Errors in SAS Programs You will make them Common ones:

Leaving off a semi-colon from the end of a SAS statement Misspelling Omitting one quote (‘) in infile or title statement

SAS Log will help you to find errors

31

Page 32: ECLT 5810 SAS Programming - Introduction

Reading External Files

data capm; * create the dataset capm;infile 'a:\TABLE.TXT'; * open the data file Table.txt;input x1 x2 m; * input the variables;

proc print; * print;var x1 x2 m; * variables;title 'CAPM Data'; * print title;

run; * run;

32