The SAS Data Step - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

The SAS Data Step

Description:

proc print data = tickets ; var limit ; run; proc sort: Sorting the Data ' ... proc print data = tickets label; format amount dollar8.2; title Speeding Ticket Data' ... – PowerPoint PPT presentation

Number of Views:63
Avg rating:3.0/5.0
Slides: 31
Provided by: nedjlati
Category:
Tags: sas | data | step | tickets

less

Transcript and Presenter's Notes

Title: The SAS Data Step


1
The SAS Data Step
  • In order to use SAS procedures to analyze
    information, your data must be in the form of a
    SAS data set.
  • This is created by entering data, reading raw
    data, or accessing files created by other
    software according to a specific syntax.
  • This lesson shows you how to design and write a
    DATA step program to create a SAS data set.

2
  • Raw Data Rows and columns.
  • Example
  • Speeding ticket fines in various states
  •   State Fine ()
  • Alabama 60
  • Delaware 31.50
  • Illinois 20
  • Idaho 12.50
  • New Mexico 20

3
  • Each row contains all the information about
    each state (name, amount).
  • In SAS nomenclature
  • Columns specify Variables
  • Rows contain the Observations.

4
  • Translation of the previous example into SAS
    syntax
  • data tickets
  • input state 1-2 amount 4-8
  • datalines
  • AL 60
  • DE 31.50
  • IL 20
  • ID 12.50
  • NM 20
  • Required Semi-colon on a line by itself ß The
    null statement
  •  
  •  
  •  

5
Rules for SAS names
  • Total number of characters 8 or less (Prior to
    SAS version 8).
  • Must start with a letter or an underscore ( _ ).
  • The remaining characters can be letters, numbers,
    or underscores.
  • Must not contain blanks.

6
Back to the Data Step
  • Specifying column locations can be omitted if and
    only if
  • at least one blank between entries,
  • all rules for SAS names are followed.

7
The input statement
  •   Contains the variables
  • name
  • type
  • location
  • For a missing value of an observation
  • enter a period (.) for missing numeric
  • enter a blank ( ) for missing name.

8
  • To read several observations displayed on one
    single line without specifying column number
    use the symbol _at__at_ at the end of the input line.
  • Example
  • SAS syntax should be
  • input state amount _at__at_
  • to read data looking like the following (on one
    line)
  • NE 30 NV 35 WA 20 WY 15

9
The datalines statement
  • Immediately follows the input statement
  • data tickets
  • input state amount _at__at_
  • datalines
  • AL 60 DE 31.50 IL 20 ID 12.50
  • NM 20 DC .

10
  • However, if the data are to be read from an
    external file (ex mydata.txt), use the infile
    statement as follows
  • data tickets
  • infile /usr/myid/myfolder/mydata.dat
  • input state amount _at__at_

11
Proc print
  • Is a procedure to display your data on screen
    (or into a file) as part of the output.
  • proc print data tickets
  • title Speeding Ticket Data
  • run

12
What it will look like (output of Proc print)
13
  • If we had more variables, (example Speed Limit
    limit), and we wanted to print only the variables
    state and limit (and not amount), then specify it
    in proc print by using the keyword var
  •  
  • proc print data tickets
  • var limit
  • run

14
proc sort Sorting the Data
  •   We can sort the data set
  • proc sort data tickets
  • by amount
  • run

15
Output after proc sort
16
Customizing the output from proc print
  •   Labeling variables provides additional info
  • data tickets
  • input state amount _at__at_
  • label amount Cost of Ticket
  • datalines
  • AL 60 DE 31.50 IL 20 ID 12.50
  • NM 20 DC .
  •  

17
Output after labeling
18
  •   Formatting values of variables proc format
  • proc format
  • value myformat AL Alabama
  • DE Delaware IL Illinois
  • IDIdaho NM New Mexico
  • DC Washington DC
  • run

19
So..
  • Labels for names of variables
  • Formats  for values of variables

20
  •   Using this custom format just created
  • data tickets
  • input state amount _at__at_
  • format state myformat.
  • datalines
  • AL 60 DE 31.50 IL 20 ID 12.50
  • NM 20 DC .
  •   Must enter a period, after the format name!

21
Built-in formats in SAS
  • Example dollar format
  • proc print data tickets
  • format amount dollar8.2
  • title Speeding Ticket Data Formatted
  • run
  •  

22
Output after dollar format
23
A list of built-in formats in SAS
  •   commaw.d numbers with commas and decimal
    points.
  •   dollarw.d numbers with dollar sign, commas,
    and decimal points.
  •   Ew. numbers in scientific notation.
  •   fracw. numbers as fractions.
  •   percentw.d numbers as percents, with
    decimal points.
  •  

24
To optimize your output look, combine both
  •   labels (for the variables) and
  •   formats (for the values of the variables)

25
  • data tickets
  • input state amount _at__at_
  • format state myformat.
  • label state State where ticket received
  • amount Cost of ticket
  • datalines
  • AL 60 DE 31.50 IL 20 ID 12.50
  • NM 20 DC .
  • proc print data tickets label
  • format amount dollar8.2
  • title Speeding Ticket Data
  • run
  •  

26
Output from previous coding
27
Note
  •   Particular to this example
  • Instead of creating the format for the States, we
    can use a built-in SAS function for the same
    purpose.
  • Names of States are already included in the SAS
    library, under the keyword stnamel

28
The code would then be
  • data tickets
  • input state amount _at__at_
  • label state State where ticket received
  • amount Cost of ticket
  • statext stnamel(state)
  • datalines
  • AL 60 DE 31.50 IL 20 ID 12.50
  • NM 20 DC .
  •  
  • proc print data tickets label
  • format amount dollar8.2
  • title Speeding Ticket Data
  • run

29
With the ouput
30
Other built-in SAS functions
  • Abs(var) var numeric
  • Log(var) var numeric
  •  
  • Round(var,unit) var unit numeric
  • Lowcase(var) var character
  • Stnamel(var) var must be 2 letter postal code
  • Upcase(var) var character
  •  
  •  
Write a Comment
User Comments (0)
About PowerShow.com