程序代写 SAS BASE PROGRAMMING

SAS BASE PROGRAMMING
– Lecture 9 –

Objectives

Copyright By PowCoder代写 加微信 powcoder

 Summarize Data
 PROC REPORT
 Selecting variables List reports Define statement Summary reports
 PROC GCHART Bar charts
 Pie charts

PROC REPORT Output

The REPORT Procedure
 PROC REPORT allows you to  Create list reports
 Create summary reports
 Enhance reports
 Request separate subtotals and grand totals

PROC REPORT vs. PROC PRINT

Create a List Report
 General form of a simple PROC REPORT step:  Example:
PROC REPORT DATA=SAS-data-set; RUN;
proc report data=data1.admit; run;

The REPORT Procedure
 The default list report displays
 Each data value as it is stored in the data set, or formatted
values if a format is stored with the data
 Variable names or labels as report column headings
 A default width for the report columns
 Character values left-justified
 Numeric values right-justified
 Observations in the order in which they are stored in the data set

Print Selected Variables
 General form of the COLUMN statement:
COLUMN SAS-variables;

Sample List Report
title ‘Salary Analysis’;
proc report data=data1.crew;
column JobCode Location Salary; run;
Partial SAS Output
JobCode Location PILOT1 LONDON FLTAT3 CARY PILOT2 FRANKFURT PILOT2 FRANKFURT FLTAT2 LONDON PILOT1 LONDON FLTAT2 FRANKFURT FLTAT2 FRANKFURT FLTAT1 LONDON FLTAT3 LONDON FLTAT2 CARY
Salary 72000 41000 81000 83000 36000 65000 35000 38000 28000 44000 37000

The DEFINE Statement
 You can enhance the report by using DEFINE statements to
 Define how each variable is used in the report
 Assign formats to variables
 Specify report column headers and column widths  Change the order of the rows in the report.

The DEFINE Statement: Options
 DISPLAY option specifies a variable to be a display variable.
 Character variables are assumed to be display variables
 ANALYSIS option specifies a variable to be an analysis variable
 Numeric variables are assumed to be analysis variables
 FORMAT= option assigns a format to a variable
 WIDTH= option controls width of report column
 ‘report-column-header’ defines the column header for a variable (similar to a label)

Enhanced List Report
proc report data=data1.crew;
column JobCode Location Salary; define JobCode / width=8 ‘Job Code’; define Location / ‘Home Base’; define Salary / format=dollar10.;
Partial SAS Output
Job Code Home Base
Salary $72,000 $41,000 $81,000 $83,000 $36,000 $65,000 $35,000 $38,000 $28,000 $44,000
PILOT2 PILOT2 FLTAT2 PILOT1 FLTAT2 FLTAT2 FLTAT1 FLTAT3
FRANKFURT FRANKFURT LONDON LONDON FRANKFURT FRANKFURT LONDON LONDON

ORDER Usage Type
 ORDER option identifies the variable(s) used to order the rows of the report
proc report data=data1.crew;
column JobCode Location Salary;
define JobCode / order width=8 ‘Job Code’; define Location / ‘Home Base’;
define Salary / format=dollar10.;

ORDER Usage Type
Partial SAS Output
Job Code Home Base
$25,000 $23,000 $21,000 $28,000 $22,000 $29,000 $36,000 $35,000 $38,000 $37,000 $34,000 $34,000 $36,000 $41,000 $44,000 $48,000 $45,000 $44,000
FRANKFURT CARY
CARY FRANKFURT LONDON LONDON FRANKFURT FRANKFURT CARY
CARY LONDON CARY
CARY LONDON FRANKFURT FRANKFURT CARY
Individual Values (not Summary Stats)

Define Group Variables
 Use the REPORT procedure to create a summary report by defining variables as group variables.
 All observations whose group variables have the same values are collapsed into a single row in the report.

Group Variables
 If you have a group variable, there must be no
display or order variables.
 Group variables produce summary reports (observations collapsed into groups).
 Display and order variables produce list reports (one row for each observation).

Analysis Variables
 Default usage for numeric variables is ANALYSIS
with a default statistic of SUM.
 If the report contains group variables, the report displays the sum of the analysis variables’ values for each group.
 If the report contains at least one display or order variable and no group variables, the report displays all of the values of the analysis variables.
 If the report contains only numeric variables, the report displays grand totals for the numeric variables.

Define Analysis Variables
 Selected statistics include:
sum (default)
number of non-missing values
Maximum Value
Minimum Value
 Example:
define Salary / mean format=dollar10.;

Summarize the Data
 Use the GROUP usage in the DEFINE statement to specify the variables that define groups.
proc report data=data1.crew;
title ‘Salary Analysis’;
column JobCode Location Salary;
define JobCode / group width=8 ‘Job Code’; define Location / group ‘Home Base’;
define Salary / analysis mean format=dollar10.;
Optional specification

Summarize the Data
FLTAT2 FLTAT3 PILOT1 PILOT2 PILOT3
Home Base CARY FRANKFURT LONDON CARY FRANKFURT LONDON CARY FRANKFURT LONDON CARY FRANKFURT LONDON CARY FRANKFURT LONDON CARY FRANKFURT LONDON
Salary Analysis
Salary $26,200 $25,000 $25,600 $35,000 $36,200 $34,333 $43,400 $44,667 $45,000 $70,333 $67,500 $70,000 $80,750 $80,000 $79,000
Mean Salary for Flight Attendant 1’s at London Base

BREAK & RBREAK Statements
 Use the BREAK statement to display a total at the beginning or end of a Group.
 Use the RBREAK statement to display the grand total at the beginning or end of the report.

Print Subtotals
 General form of the BREAK statement:  Selected options:
BREAK BEFORE|AFTER /;
SUMMARIZE Prints the total.
OL Prints a single line above the total. DOL Prints a double line above the total. UL Prints a single line below the total. DUL Prints a double line below the total.

BREAK Example
proc report data=data1.crew;
column JobCode Location Salary;
define JobCode / group width=8 ‘Job Code’; define Location / group ‘Home Base’; define Salary / format=dollar10.;
break after JobCode / summarize dol dul;

BREAK Example Output
Job Code Home Base Salary
======== FLTAT1 ======== FLTAT2
======== FLTAT2 ======== FLTAT3
======== FLTAT3 ========
CARY $131,000
CARY FRANKFURT LONDON
CARY FRANKFURT LONDON
$128,000 ========== $359,000 ========== $245,000 $181,000 $206,000 ========== $632,000 ========== $217,000 $134,000 $180,000 ========== $531,000 ==========
BREAK (Summarize)
DOL – Double Overline
DUL – Double Underline

Print Grand Totals
 General form of the RBREAK statement:  Selected options:
RBREAK BEFORE|AFTER /;
SUMMARIZE Prints the total.
OL Prints a single line above the total. DOL Prints a double line above the total. UL Prints a single line below the total. DUL Prints a double line below the total.

RBREAK Example
proc report data=data1.crew;
column JobCode Location Salary;
define JobCode / group width=8 ‘Job Code’; define Location / group ‘Home Base’; define Salary / format=dollar10.;
rbreak after / summarize dol;

RBREAK Example Output
FLTAT2 FLTAT3 PILOT1 PILOT2 PILOT3
Home Base CARY FRANKFURT LONDON CARY FRANKFURT LONDON CARY FRANKFURT LONDON CARY FRANKFURT LONDON CARY FRANKFURT LONDON CARY FRANKFURT LONDON
Salary $131,000 $100,000 $128,000 $245,000 $181,000 $206,000 $217,000 $134,000 $180,000 $211,000 $135,000 $210,000 $323,000 $240,000 $158,000 $300,000 $205,000 $294,000 ========== $3,598,000
DOL – Double Overline
RBREAK (Summarize)

Enhance the Report
 We can further enhance our reports with the headline and headskip options
 Headline places a line underneath the column headers
 Headskip places a blank line underneath the column headers
proc report data=data1.crew headline headskip; column JobCode Location Salary;
define JobCode / group width=8 ‘Job Code’; define Location / group ‘Home Base’; define Salary / format=dollar10.;
rbreak after / summarize dol; run;

Example Output
Job Code Home Base Salary ƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒƒ
FLTAT1 CARY
LONDON FLTAT2 CARY
LONDON FLTAT3 CARY
LONDON PILOT1 CARY
PILOT2 CARY FRANKFURT
PILOT3 CARY
FRANKFURT LONDON
$100,000 $128,000 $245,000 $181,000 $206,000 $217,000 $134,000 $180,000 $211,000 $135,000 $210,000 $323,000 $240,000 $158,000 $300,000 $205,000 $294,000
==========
$3,598,000
Head Skip (Blank Line)

PROC REPORT vs. PROC TABULATE

Summarize with Charts
 In the GCHART procedure, you can do the following:
 Specify the physical form of the chart.
 Specify a chart variable that determines the number of bars or pie slices to create.
 Optionally specify an analysis variable to use for calculating statistics that determine the height (or length) of the bar or the size of the slice.

The GCHART Procedure
 General form of the PROC GCHART statement:
 Use one of these statements to specify the desired type of chart:
PROC GCHART DATA=SAS-data-set; RUN;
VBAR chart-variable . . . ; HBAR chart-variable . . . ; PIE chart-variable . . . ;

Chart Variable
 The chart variable
 Determines the number of bars or slices produced within a
 Can be character or numeric

Vertical Bar Chart

Vertical Bar Chart Example
 Using the crew data set in data1, produce a vertical bar chart
that displays the number of employees in each job code.  Solution
proc gchart data=data1.crew;
vbar JobCode;

Vertical Bar Chart Example Output

Horizontal Bar Chart

Horizontal Bar Chart
 Using the crew data set in data1, produce a horizontal bar
chart that displays the number of employees in each job code.  Solution
proc gchart data=data1.crew; hbar JobCode;

Horizontal Bar Chart

Chart Numeric Variables
 GCHART can graph character or numeric variables.
 When graphing numeric variables, by default, the values are grouped together to create a smaller number of lines.
 To override the default behavior for numeric chart variables, use the DISCRETE option in the VBAR, HBAR, or PIE statement.

Numeric Chart: Default Grouping
proc gchart data=data1.crew;
vbar salary;

Numeric Chart: DISCRETE Option
proc gchart data=data1.crew;
vbar salary / discrete;

Analysis Variables
 An analysis variable can be used to chart summary statistics.
 Example:
 Produce a vertical bar chart that displays the average salary of employees in each job code.
proc gchart data=data1.crew;
vbar JobCode / sumvar=Salary type=mean;

Chart An Analysis Variable

Chart Summary Statistics
 You can chart a summary statistic (e.g., mean) using the SUMVAR= and TYPE= options
 General form:  Description:
specifies the analysis variable to use for the sum or mean calculation.
specifies that the height or length of the bar or size of the slice represents a mean or sum of the analysis-variable values.

Pie Chart Example
 Use the crew data set in the data1 folder
 Produce a pie chart that displays the sum of the salaries for each job code
 Format the salaries appropriately
 Fill the pie chart with a grid pattern

Fill Option
proc gchart data=data1.crew;
pie JobCode / sumvar=Salary
format Salary dollar8.; run;

Pie Chart Example
 Use the crew data set in the data1 folder
 Produce a pie chart that displays sum of the salaries for each job code
 Format the salaries appropriately
 Fill the pie chart with a solid pattern
 Bring the slice for ‘PILOT3’ out to emphasize it

Explode Option
proc gchart data=data1.crew;
pie JobCode / sumvar=Salary
explode = ‘PILOT3’;
format Salary dollar8.; run;

Class Exercise 1
 Use the employees data set in the data1 folder  Create a report using the REPORT procedure
 Display the following variables  Country
 LastName
 Display the observations sorted by Country
 Change the column header of LastName to ‘Last Name’  Format the Salary appropriately

Class Exercise 2
 Use the heart data set in the data1 folder  Use the REPORT procedure
 Display the following variables:  Survive
 Arterial
 Create a summary report grouped by Survive and Sex for the mean Arterial values
 Use the following column headers  Survived
 Mean Arterial Pressure

Class Exercise 2 – continued
 Format the Sex variable as follows:  1=Female
 Keep 2 decimal places for the mean arterial values
 Specify a column width of 8 for Survive and Arterial
 Place a header line below the column headers
 Display the subtotal mean Arterial value at the end of each Survive group; place a double line above this value and a single line below this value
 Display the total mean Arterial value at the end of the report

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com