+ All Categories
Home > Documents > SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET...

SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET...

Date post: 02-Aug-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
57
SAS Essentials 2 or How to Train Your SAS ® Data Sets 1 Version Date: September 2019 Tasha Chapman Fundamentals of DATA steps and data manipulation
Transcript
Page 1: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

SAS Essentials 2 or How to Train Your SAS ® Data Sets

1 Version Date:

September 2019 Tasha Chapman

Fundamentals of DATA steps and data manipulation

Page 2: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

The elements of a SAS program

2

Page 3: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

3

DATA steps:

Read and write data, manipulate

data, perform calculations, etc.

Every DATA step creates a new

dataset

Page 4: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

4

PROC steps:

Produce reports, summarize data, sort data,

create formats, generate graphs, etc. etc. etc.

Every SAS

program can

have multiple

DATA and PROC

steps

Page 5: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

DATA step examples

5

Page 6: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

6

DATA MyCars

“DATA MyCars” creates a dataset called “MyCars”

“SET sashelp.cars” references the dataset “cars” in the SAShelp

library as the basis for the new dataset

“SalePrice =” creates a new variable called “SalePrice” that is

calculated as shown. This is called an assignment statement.

“Format” applies a format to the newly created variable. This will

display the number as American currency.

Page 7: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

DATA step examples

7

Most of your DATA steps will look something like this:

Creates a new

dataset called

“Females” Uses previous

dataset “AllData”

as the basis of the

new dataset

Applies these modifications to the new

dataset

Page 8: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

DATA step examples

8

Name Gender Dept

Michael M Manager

Pam F Admin

Jim M Sales

Dwight M Sales

Stanley M Sales

Kevin M Accounting

Angela F Accounting

Phyllis F Sales

AllData

Name Gender Dept

Pam F Admin

Angela F Accounting

Phyllis F Sales

Females

Page 9: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Where did my data come from?

9

Page 10: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

10

Depending on the environment,

might be called

“Files and Folders” or “Libraries”

Page 11: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

11

SAS library

SAS data set

Page 12: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

SAS libraries

LIBNAME statement assigns a libref

Libref (short for “Library Reference”) is an alias or

nickname for a directory or folder for SAS datasets

Libname statements are global statements

12

Page 13: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

SAS libraries

13

Need to tell SAS where to find

these data sets

Would assign a libref using the

LIBNAME statement

Page 14: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

SAS libraries

14

LIBNAME statement:

Assigns a libref

Libref is an alias for a directory or folder where you store

permanent SAS datasets

Libref can be anything you choose

Libref only exists for current SAS session

Page 15: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

LIBNAME examples

15

Page 16: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

SAS libraries

LIBNAME statement assigns a libref

Libref (short for “Library Reference”) is an alias or

nickname for a directory or folder for SAS datasets

Libname statements are global statements

Dataset references contain two parts:

libref

dataset-name

Looks like: libref.dataset-name

If libref is blank, the default is the Work library

16

Page 17: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

SAS libraries

17

Data set reference:

Consists of two parts –

Libref.dataset-name

mydata.survey_data is short for

T:\RA\RESEARCH_TEAM\_a_resources\SAS\SAS Datasets\survey_data

Default is Work

Page 18: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

SAS Work library

Work is a temporary library

SAS datasets created in Work only exist during SAS session

Once SAS session ends, datasets are erased

Do not need to assign a libref for Work or specify it in

dataset references

data Test_Scores;

is the same as data work.Test_Scores;

18

Page 19: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

SAShelp library

SAShelp is a permanent library that comes with SAS

Contains basic training data sets

Find more info at https://support.sas.com/documentation/tools/sashelpug.pdf

19

Page 20: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

How do I get my data into SAS?

20

Page 21: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

21

Page 22: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

22

Page 23: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

23

Page 24: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

24

Page 25: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

25

Page 26: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

26

Page 27: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Four variables: Gender, Age, Height (in inches), Weight (in

pounds)

Variables separated by blanks

Reading data from a text file

27

Page 28: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

INFILE – where to find the data

INPUT – variable names to associate with each data value

($ indicates character variable. Otherwise numeric.)

28

Reading data from a text file

Page 29: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

How do I explore my data?

29

Page 30: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

PROC Contents

PROC Contents can be used to display the metadata

(descriptor portion) of the SAS dataset

30

proc contents data=sashelp.baseball;

run;

Page 31: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Results of PROC Contents of

“SAShelp.BASEBALL”

31

PROC Contents

Page 32: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Number of

observations

and variables

Dataset name

File name 32

PROC Contents

Page 33: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

PROC Contents variable list

# - Variable number (varnum)

Variable – Name of variable

Type – Numeric or Character

Len – Variable length

Label – Descriptive label

Format – How the data is

displayed

Informat – How the data was

read by SAS

33

Page 34: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

PROC Contents

Learn more about uses for PROC Contents…

34

Writing Code With Your Data

Joe Matise

Thursday, 2:00-3:00pm

Seattle

Page 35: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

PROC Print

PROC Print can be used to list the data in a SAS dataset

35

proc print data=sashelp.baseball;

run;

Page 36: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Results of PROC Print of “Demographics”

Obs – short for “observation” (part of PROC Print output)

Numbers observations from 1 to N

36

PROC Print

Page 37: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Data Management

Learn more about sharing and managing data

37

‘Tis Better to Give Than Receive: Considerations When

Sharing Data

Melissa Pfeiffer

Friday, 10:00-10:30am

Bellevue II

Page 38: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

What can we do in a DATA step?

38

Page 39: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Creating variables

Assignment statements can be used to create variables

39

data mybaseball; set sashelp.baseball;

percentHR = (nHome / nRuns)*100;

run;

Page 40: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Functions

Functions perform calculations or transformations

40

data mybaseball; set sashelp.baseball;

percentHR = round(((nHome / nRuns)*100),.01);

run;

Page 42: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Functions

Learn more about functions…

42

A Survey of Some of the Most Useful SAS Functions

Ron Cody

Thursday, 3:30-4:30pm

Seattle

Page 43: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Functions

Learn more about functions…

43

Fifteen Functions to Supercharge Your SAS Code

Josh Horstman

Friday, 9:00-10:00am

Rainier

Page 44: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Functions

Learn more about functions…

44

Counting the Work Days Until

Robert Ellsworth

Friday, 11:00-11:30am

Seattle

Page 45: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Subsetting datasets

Can use IF or WHERE statements to only include

observations you need

45

data mybaseball; set sashelp.baseball;

where league = 'National';

run;

Page 46: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Subsetting datasets

Can use either IF or WHERE in a DATA step with SET

statement

In both examples “Minors” dataset will only include

observations where age is less than 18

46

Page 47: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Subsetting datasets

Think of if age lt 18; as short for

if age lt 18 then output;

Can output to multiple datasets using IF/THEN logic

47

Page 48: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Subsetting datasets

Use a WHERE statement in a PROC step to only include

selected observations

“Test_data” dataset still includes all observations

Only observations where league is “American” will be

included in the calculations and output of the procedure

48

Page 49: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Merging Data Sets

Learn about merging and joining…

49

Merge with Caution: How to Avoid Common Problems when

Combining SAS Datasets

Josh Horstman

Thursday, 11:00-12:00pm

Seattle

Page 50: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

If, Then, Else

IF <condition> THEN <X>;

ELSE <Y>;

If Score >= 70 Then Grade = 'Passing Grade';

Else Grade = 'Failing Grade';

Student Score Grade

Jane 75 Passing Grade

Dave 56 Failing Grade

Jack 90 Passing Grade

Sue 68 Failing Grade 50

Page 51: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

If, Then, Else

IF <condition> THEN <X>;

ELSE <Y>;

If Score >= 70 Then Grade = 'Passing Grade';

Else Grade = 'Failing Grade';

Student Score Grade

Jane 75 Passing Grade

Dave 56 Failing Grade

Jack 90 Passing Grade

Sue 68 Failing Grade

Assignment statement

Used to create new variables

51

Page 52: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

If, Then, Else

IF <condition> THEN <X>;

ELSE IF <condition2> THEN <Y>;

ELSE <Z>;

If Score >= 70 Then Grade = 'Passing Grade';

Else If 60 <= Score <= 69 Then Grade = 'Incomplete';

Else Grade = 'Failing Grade';

Student Score Grade

Jane 75 Passing Grade

Dave 56 Failing Grade

Jack 90 Passing Grade

Sue 68 Incomplete 52

Page 53: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

If, Then, Else

When using ELSE IF:

Processes IF-THEN conditions until first true statement is met,

then it moves on to the next observation

Once a condition is met, the observation is not reevaluated

53

Page 54: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Arithmetic operators

Arithmetic Symbol Example

Addition + Xplus = 4+2;

Subtraction – Xminus = 4-2;

Multiplication * Xmult = 4*2;

Division / Xdiv = 4/2;

Exponents ** Xexp = 4**2;

Negative numbers – Xneg = -2;

54

Page 55: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Comparison operators

Logical comparison Mnemonic Symbol

Equal to EQ =

Not equal to NE ^= or ~=

Less than LT <

Less than or equal to LE <=

Greater than GT >

Greater than or equal to GE >=

Equal to one in a list IN

Not equal to any in a list NOT IN

55

Note: <> can be used for not equal to, but only in the WHERE statement or PROC SQL

Page 56: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Logical operators

Boolean operator

And

Or

Not

56

Page 57: SAS Essentials 1 · DATA MyCars “DATA MyCars” creates a dataset called “MyCars” “SET sashelp.cars” references the dataset “cars” in the SAShelp library as the basis

Coming up next

SAS Essentials II

57


Recommended