Chapter 4

advertisement
Chapter 4: Creating List Reports
1.
2.
3.
4.
5.
6.
7.
Objectives:
Specify SAS data sets to print
Select variables and observations to print
Sort data by values of one or more variables
Specify column totals for numeric variables
Add titles and footnotes to output
Assign variable labels
Apply formats to values of variables
1
Overview of PROC PRINT
List reports are typically generated with the PRINT procedure.
The SAS System
Obs
Emp
ID
LastName
FirstName
Job
Code
1
2
3
4
5
6
7
8
0031
0040
0071
0082
0091
0106
0355
0366
GOLDENBERG
WILLIAMS
PERRY
MCGWIER-WATTS
SCOTT
THACKER
BELL
GLENN
DESIREE
ARLENE M.
ROBERT A.
CHRISTINA
HARVEY F.
DAVID S.
THOMAS B.
MARTHA S.
PILOT
FLTAT
FLTAT
PILOT
FLTAT
FLTAT
PILOT
PILOT
Salary
50221.62
23666.12
21957.71
96387.39
32278.40
24161.14
59803.16
120202.38
2
Overview of PROC PRINT
You can display
 titles and footnotes
 descriptive column headings
 formatted data values.
Salary Report
Obs
Emp
ID
LastName
FirstName
Job
Code
1
2
3
4
5
6
7
8
0031
0040
0071
0082
0091
0106
0355
0366
GOLDENBERG
WILLIAMS
PERRY
MCGWIER-WATTS
SCOTT
THACKER
BELL
GLENN
DESIREE
ARLENE M.
ROBERT A.
CHRISTINA
HARVEY F.
DAVID S.
THOMAS B.
MARTHA S.
PILOT
FLTAT
FLTAT
PILOT
FLTAT
FLTAT
PILOT
PILOT
Annual
Salary
$50,221.62
$23,666.12
$21,957.71
$96,387.39
$32,278.40
$24,161.14
$59,803.16
$120,202.38
3
Overview of PROC PRINT
You can display
 column totals
 column subtotals
 page breaks for each subgroup.
The SAS System
------------------------ JobCode=FLTAT -------------------------
Obs
1
2
3
4
------JobCode
Emp
ID
LastName
FirstName
Salary
0040
0071
0091
0106
WILLIAMS
PERRY
SCOTT
THACKER
ARLENE M.
ROBERT A.
HARVEY F.
DAVID S.
23666.12
21957.71
32278.40
24161.14
--------102063.37
4
Overview of PROC PRINT
The SAS System
------------------------ JobCode=PILOT -------------------------
Obs
5
6
7
8
------JobCode
Emp
ID
LastName
FirstName
Salary
0031
0082
0355
0366
GOLDENBERG
MCGWIER-WATTS
BELL
GLENN
DESIREE
CHRISTINA
THOMAS B.
MARTHA S.
50221.62
96387.39
59803.16
120202.38
--------326614.55
=========
428677.92
5
Basic Report
General Syntax:
PROC PRINT DATA=SAS-data-set;
RUN;
• List all variables and all observations in the data set
• A column for observation numbers appears on the far left.
• Variables appear in the order in which they occur in the data
set.
Example:
libname mylib ‘c:\math707\sasdata’;
Proc print data = mylib.heart;
Run;
Creating a Basic List Report with
selected variables
General form of the PRINT procedure:
PROC PRINT DATA=SAS-data-set;
VAR variable(s) ;
RUN;
Example:
libname mylib ‘c:\math707\sasdata';
proc print data=mylib.heart;
VAR sex heart cardiac;
run;
7
Removing the Obs Column
The NOOBS option suppresses the row
numbers on the left side of the report.
General form of the NOOBS option:
PROC PRINT DATA=SAS-data-set NOOBS;
RUN;
Example:
libname mylib ‘c:\math707\sasdata‘ NOOBS;
proc print data=mylib.heart;
VAR sex heart cardiac;
run;
8
Identifying Observations in PROC PRINT procedure
Using ID statement in PROC PRINT procedure:
General form for ID statement:
ID variable(s);
Example:
libname mylib ‘c:\math707\sasdata';
proc print data=mylib.heart;
VAR sex heart cardiac;
ID patient sex;
run;
• The variables patient and sex are displayed first in each row,
followed by the variables in the VAR statement.
• No Obs column appears.
• If a variable appears in both ID and VAR statements, the
variable will be displayed TWICE. Therefore, a variable in the
ID statement does not need to be in the VAR statement.
9
Exercise
The SAS data set Insure is posted on our class webpage.
Variable
ID
Name
Policy
Company
PctInsured
Total
BalanceDue
ID
Name
2458 Murray, W
2462 Almers, C
2501 Bonaventure, T
Type
num
char
num
char
num
num
num
Length
8
20
5
11
3
8
8
Description
client ID number
client name
client policy number
name of company
percent insured
total balance
balance due
PctInsur
ed
32668 MUTUALITY
100
95824 RELIABLE
80
87795 A&R
80
Policy Company
Total
98.64
780.23
47.38
BalanceDue
0.00
156.05
9.48
10
Exercise
Write a program to read the Insure data, and print the
data using different proc statements, respectively:
1. Basic listing print,
2. Do not print OBS column, and print only variables ID,
NAME, Total, BalanceDue
3. Use BalanceDue and Name as ID, and print
BalanceDue, Name, Policy, PctInsured and Total
with BalanceDue and Name in the output only once.
Save the program, C4_Print1 in SASEx folder
11
Answer
Libname mylib ‘c:\math707\sasdata’;
Proc print data= mylib.insure; run;
Proc print data=mylib.insure noobs;
VAR ID, NAME, Total, BalanceDue;
Run;
Proc print data=mylib.insure;
ID Balancedue Name;
VAR Policy, PctInsured Total;
Run;
12
Selecting observations:
use WHERE Statement to produce a report
of a subset of the data set
The WHERE statement
 enables you to select observations that meet a certain
condition
 can be used with most SAS procedures.
13
General form of the WHERE statement:
WHERE where-expression;
where-expression specifies the conditions for selecting observation.
It is a sequence of operands and operators.
Operands include
–
–
variables
constants.
Operators include
–
–
–
–
comparison operators
logical operators
special operators
functions.
14
Comparison Operators used in Where statement
Examples of Where statement:
where Salary>25000;
where EmpID='0082';
where Salary=.;
where LastName=' ';
where JobCode in('PILOT','FLTAT');
where JobCode in('PILOT' 'FLTAT');
Character comparisons are case-sensitive.
The IN operator allows commas or blanks to separate
values.
15
Comparison Operators
Mnemonic Symbol
Definition
EQ
=
equal to
NE
not equal to
GT
^=
¬=
~=
>
LT
<
less than
GE
>=
greater than or equal to
LE
<=
less than or equal to
IN
greater than
equal to one of a list
16
Logical Operators
Logical operators include
AND
if both expressions are true, then the
compound expression is true
&
where JobCode='FLTAT' and Salary>50000;
OR
if either expression is true, then the
|
compound expression is true
where JobCode='PILOT' or JobCode='FLTAT';
NOT
can be combined with other operators
to reverse the logic of a comparison.
^
where JobCode not in('PILOT','FLTAT');
17
Special Operators used in Where statement
Special operators include
BETWEEN-AND
selects observations in which the
value of the variable falls within a
range of values, inclusively.
where Salary between 50000 and 70000;
CONTAINS
selects observations that include
the specified substring.
?
where LastName ? 'LAM';
(LAMBERT, BELLAMY, and ELAM are selected.)
18
Special Operators used in Where statement
Additional special operators supported by the
WHERE statement are
– LIKE
– sounds like
– IS MISSING (or IS NULL).
19
Special Operators used in Where statement
LIKE : selects observations by comparing character
values to specified patterns.
A percent sign (%) replaces any number of
characters.
An underscore (_) replaces one character.
where Code like 'E_U%';
Selects observations where the value of Code begins
with an E, followed by a single character, followed by a
U, followed by any number of characters.
20
Special Operators used in Where statement
The sounds like (=*) operator selects observations that
contain spelling variations of the word or words
specified.
where Name=*'SMITH';
Selects names like SMYTHE and SMITT.
IS NULL or IS MISSING selects observations in which the
value of the variable is missing.
where Flight is missing;
where Flight is null;
21
Printing Selected Observations
Use the WHERE statement to control which observations
are processed.
EmpID
LastName
0031
GOLDENBERG DESIREE
0040
0071
Select
rows to
print
FirstName JobCode
Salary
PILOT
50221.62
WILLIAMS
ARLENE M. FLTAT
23666.12
PERRY
ROBERT A. FLTAT
21957.71
PROC Step
proc print data=mylib.empdata noobs;
var JobCode EmpID Salary;
where JobCode='PILOT';
run;
The SAS System
Job
Code
Emp
ID
PILOT
PILOT
PILOT
PILOT
0031
0082
0355
0366
Salary
50221.62
96387.39
59803.16
120202.38
22
More examples of Where statement
for selecting observations
WHERE actlevel = ‘LOW’ or actlevel = ‘HIGH’;
WHERE actlevel in (‘LOW’ , ‘HIGH’);
WHERE (age >= 40 and pulse > 80) or actlevel = ‘HIGH’;
WHERE firstname contains ‘SAM’;
Where firstname ? ‘SAM’;
Where salary between 50000 and 80000 ;
23
Exercise: Admit data
Variable
ID
Name
Sex
Age
Date
Height
Weight
ActLevel
Fee
Type
Length
8
20
1
8
8
8
8
4
8
num
char
char
num
num
num
num
char
num
ID
Name
2458 Murray, W
2462 Almers, C
2501 Bonaventure, T
Sex
M
F
F
Description
patient ID number
patient name
sex (F or M)
age in years
day of admission
height in inches
weight in pounds
activity level (LOW, MOD, HIGH)
clinic admission fee
Age Date Height Weight ActLevel Fee
27
1
72
168 HIGH
85.20
34
3
66
172 HIGH
124.80
31
17
61
123 LOW
149.75
24
Exercise
Write a SAS program to read the Admit data and do the following
tasks:
Print the data with only ActLevel to be LOW or HIGH.
Print the data with Age >= 40 and Weight between 160 and 200
Print the data, do not print the OBS column, use Name as ID, print
only variables Name, Age, ActLevel and Fee, only print individuals
with Name starting with M or name starting with K.
Save the program, C4_print2 to your SASEx folder
25
Answer
options firstobs=1 obs=max;
proc print data = mylib.admit;
where actlevel in ('LOW' 'HIGH');
run;
proc print data = mylib.admit;
where age ge 40 and weight between 160 and 200;
run;
proc print data=mylib.admit noobs;
ID name;
VAR name age actlevel fee;
where name like 'M%' or name like 'K%';
run;
26
Sorting a SAS Data Set
By default, PROC PRINT lists the data in the order in which
they appear in the data set. To sort your report based in
values of a variable, you must sort the data using PROC
SORT procedure before using PRINT procedure.
The PROC SORT procedure
– rearranges the observations in a SAS data set
– can create a new SAS data set containing the
rearranged observations
– can sort on multiple variables
– can sort in ascending (default) or descending order
– does not generate printed output
– treats missing values as the smallest possible value.
27
Sorting a SAS Data Set
General form of the PROC SORT step:
PROC SORT DATA=input-SAS-data-set
<OUT=output-SAS-data-set>;
BY <DESCENDING> by-variable(s);
RUN;
Examples:
proc sort data=mylib.empdata;
by Salary;
run;
proc sort data=mylib.empdata out=work.jobsal;
by JobCode descending Salary;
run;
28
PROC SORT
• If you do not use OUT = option, the sorted data set will replace the
original data set permanently.
• By default, data is sorted in ascending order based on the variables(s) in
the BY statement.
• The key word DESCENDING option sort the data in descending
Example:
PROC SORT data = mylib.admit out = mylib.admit_Sort;
BY DESCENDING actlevel age;
RUN;
The data set mylib.admit_sort is sorted by actlevel in descending order and
then AGE in ascending order.
NOTE: DESCENDING only has effect for the variable following the word.
29
Generating Column Totals
The SUM statement produces column totals.
General form of the SUM statement:
SUM variable(s);
The SUM statement also produces subtotals if you print
the data in groups.
30
Generating Column Totals
EmpID
LastName
FirstName JobCode
0031
GOLDENBERG DESIREE
0040
0071
Salary
PILOT
50221.62
WILLIAMS
ARLENE M. FLTAT
23666.12
PERRY
ROBERT A. FLTAT
21957.71
PROC Step
Produce proc print data=mylib.empdata noobs;
var JobCode EmpID Salary;
column
sum Salary;
run;
totals
The SAS System
Job
Code
Emp
ID
PILOT
FLTAT
FLTAT
0031
0040
0071
Salary
50221.62
23666.12
21957.71
.
.
=========
428677.92
Requesting Subtotals in PROC PRINT
General form: BY statement in the PRINT procedure:
BY <DESCENDING> variable1 <DESCENDING> variable2 …….
<NOSORTED> ;
• NOSORTED option specifies that observations are not
necessarily sorted in order. If observations that have the same
values for the BY variables are not contiguous, the procedure
treats each contiguous set as a separate group.
• If you DO NOT use NOSORTED option, then, the data set MUST
be sorted by using PROC SORT prior to using the BY statement
in the PROC PRINT procedure.
• DESCENDING only affect the variable followed.
• If you want two variables to be sorted, both in DESCENDING
order, you need to specify DESCENDING ahead of each variable,
respectively.
32
Requesting Subtotals and Grand Totals
• Print the data set grouped by JobCode with a subtotal for the
Salary column for each JobCode.
• The data must be sorted in order to print subtotal based on the
variable in the BY statement in the following example.
proc sort data=mylib.empdata
out=work.empdata_s;
by JobCode;
run;
proc print data=work.empdata_s;
by JobCode;
sum Salary;
run;
Using a BY statement and a SUM statement together in a
PROC PRINT step produces subtotals and grand totals.
Printing Subtotals and Grand Totals
The SAS System
------------------------ JobCode=FLTAT ------------------------Obs
1
2
3
4
------JobCode
Emp
ID
LastName
FirstName
Salary
0040
0071
0091
0106
WILLIAMS
PERRY
SCOTT
THACKER
ARLENE M.
ROBERT A.
HARVEY F.
DAVID S.
23666.12
21957.71
32278.40
24161.14
--------102063.37
------------------------ JobCode=PILOT ------------------------Obs
5
6
7
8
------JobCode
Emp
ID
LastName
FirstName
Salary
0031
0082
0355
0366
GOLDENBERG
MCGWIER-WATTS
BELL
GLENN
DESIREE
CHRISTINA
THOMAS B.
MARTHA S.
50221.62
96387.39
59803.16
120202.38
--------326614.55
=========
428677.92
34
Requesting Subtotal in Separate Pages
PAGEBY statement to print each subgroup on a separate page.
General form of the PAGEBY statement:
PAGEBY by-variable;
proc print data=mylib.empdata;
by JobCode;
pageby JobCode;
sum Salary;
run;
The PAGEBY statement must be used with a BY statement.
If the BY variable is in Descending order, your need:
BY descending variable-name;
PAGEBY variable-name;
NOTE: Do not need to add descending in PAGEBY statement.
35
Print Subtotal in separate pages
The SAS System
------------------------ JobCode=FLTAT -------------------------
Obs
1
2
3
4
------JobCode
Emp
ID
LastName
FirstName
Salary
0040
0071
0091
0106
WILLIAMS
PERRY
SCOTT
THACKER
ARLENE M.
ROBERT A.
HARVEY F.
DAVID S.
23666.12
21957.71
32278.40
24161.14
--------102063.37
36
Print Subtotal in separate pages
The SAS System
------------------------ JobCode=PILOT -------------------------
Obs
5
6
7
8
------JobCode
Emp
ID
LastName
FirstName
Salary
0031
0082
0355
0366
GOLDENBERG
MCGWIER-WATTS
BELL
GLENN
DESIREE
CHRISTINA
THOMAS B.
MARTHA S.
50221.62
96387.39
59803.16
120202.38
--------326614.55
=========
428677.92
37
Exercise
Write a SAS program to read Admit data and do the following
tasks for your report:
1. Sort the data by ActLevel in descending order, and save the
sorted data in WORK library with name Admit_s.
2. Print the data set with the subtotal and grand total of Fee
variable for each Actlevel in Descending order, and only print
variables Name, Age and Fee.
3. Print the data with the subtotal and grand total of Fee
variable for each Actlevel in Descending order, print only
variables Name, Age and Fee, then print each subgroup of
ActLevel in different pages.
Save the program C4_Print_sort_Subtotal in SASEx
folder
38
Answer
PROC SORT data=mylib.admit out=admit_s;
by descending ActLevel; run;
proc print data=admit_s;
by descending actlevel;
var Name age fee;
sum fee;
run;
proc print data=admit_s;
by descending actlevel;
pageby actlevel;
var name age fee;
sum fee;
run;
39
Creating a customized layout with BY
Groups and ID variables
When the ID and BY statements specify the
same variable,
– the Obs column is suppressed
– the BY line is suppressed
– the ID/BY variable prints in the leftmost column
– each ID/BY value only prints at the start of each BY
group (and on the subtotal line, if a SUM
statement is used).
40
Creating a customized layout with BY
Groups and ID variables
Specify JobCode in the BY and ID statements to
change the report format.
proc sort data=mylib.empdata out=mylib.empdata_s;
by JobCode;
run;
proc print data=mylib.empdata_s;
by JobCode;
id JobCode;
sum Salary;
run;
41
Creating a customized layout with BY Groups
and ID variables
The SAS System
Job
Code
Emp
ID
LastName
FirstName
Salary
FLTAT
0040
0071
0091
0106
WILLIAMS
PERRY
SCOTT
THACKER
ARLENE M.
ROBERT A.
HARVEY F.
DAVID S.
23666.12
21957.71
32278.40
24161.14
--------102063.37
0031
0082
0355
0366
GOLDENBERG
MCGWIER-WATTS
BELL
GLENN
DESIREE
CHRISTINA
THOMAS B.
MARTHA S.
50221.62
96387.39
59803.16
120202.38
--------326614.55
=========
428677.92
----FLTAT
PILOT
----PILOT
42
Double Spacing List Output
One can control the listing output in double spacing
by using the option: DOUBLE in PROC PRINT
statement:
PROC PRINT Data = mylib.admit double;
Var id age fee;
Where actlevel = ‘HIGH’;
Run;
NOTE: DOUBLE option does not affect HTML
output.
43
Exercise
Write a SAS program to read Admit data and do the following
tasks for your report:
1. Sort the data by ActLevel in descending order, and save the
sorted data in WORK library with name Admit_s.
2. Print the data set with the subtotal and grand total of Fee
variable for each Actlevel in Descending order, also use
Actlevel as the ID variable, and only print variables Name, Age
and Fee.
3. Same as (2) with output in DOUBLE space.
Save the program C4_Print_ID_BY in SASEx folder
44
Answer
PROC SORT data=mylib.admit out=admit_s;
by descending ActLevel; run;
proc print data=admit_s;
by descending actlevel;
ID actlevel;
var Name age fee;
sum fee;
run;
proc print data=admit_s double;
by descending actlevel;
ID actlevel;
var Name age fee;
sum fee;
run;
45
Defining Titles and Footnotes
•You use titles and footnotes to enhance reports.
•General form of the TITLE statement:
TITLEn 'text ';
General form of the FOOTNOTE statement:
FOOTNOTEn 'text ';
Examples:
title1 'Flight Crew Employee Listing';
footnote2 'Employee Review';
46
Defining Titles and Footnotes
•Features of titles:
– Titles appear at the top of the page.
– The default title is The SAS System.
– The value of n can be from 1 to 10.
– An unnumbered TITLE is equivalent to TITLE1.
– Titles remain in effect until they are changed,
cancelled, or you end your SAS session.
– The null TITLE statement, title;, cancels all
titles.
47
Defining Titles and Footnotes
•Features of footnotes:
– Footnotes appear at the bottom of the page.
– No footnote is printed unless one is specified.
– The value of n can be from 1 to 10.
– An unnumbered FOOTNOTE is equivalent to
FOOTNOTE1.
– Footnotes remain in effect until they are
changed, cancelled, or you end your SAS
session.
– The null FOOTNOTE statement, footnote;,
cancels all footnotes.
48
Changing Titles and Footnotes
•TITLEn or FOOTNOTEn
– replaces a previous title or footnote with the same
number
– cancels all titles or footnotes with higher
numbers.
49
Defining Titles and Footnotes
PROC PRINT Code
proc print data=work.march;
title1 'The First Line';
title2 'The Second Line';
run;
Resultant Title(s)
proc print data=work.march;
title2 'The Next Line';
run;
proc print data=work.march;
title 'The Top Line';
run;
proc print data=work.march;
title3 'The Third Line';
run;
proc print data=work.march;
title;
run;
50
Defining Titles and Footnotes
PROC PRINT Code
proc print data=work.march;
title1 'The First Line';
title2 'The Second Line';
run;
Resultant Title(s)
The First Line
The Second Line
proc print data=work.march;
title2 'The Next Line';
run;
The First Line
The Next Line
proc print data=work.march;
title 'The Top Line';
run;
The Top Line
proc print data=work.march;
title3 'The Third Line';
run;
The Top Line
The Third Line
proc print data=work.march;
title;
run;
51
...
Assigning Descriptive Labels
•General form of the LABEL statement:
LABEL variable='label'
variable='label';
•'label'
specifies a label up to 256 characters.
•Labels are used
– to replace variable names in SAS output
– automatically by many procedures
– In the PRINT procedure when the LABEL or
SPLIT= option is specified in the PROC PRINT
statement.
52
Assigning Temporary Labels to Variables
mylib.empdata
EmpID
LastName
FirstName JobCode
0031
GOLDENBERG DESIREE
0040
0071
Salary
PILOT
50221.62
WILLIAMS
ARLENE M. FLTAT
23666.12
PERRY
ROBERT A. FLTAT
21957.71
PROC Step
proc print data=mylib.empdata label;
label LastName='Last Name'
FirstName='First Name'
Salary='Annual Salary';
title1 'Salary Report';
run;
Salary Report
Obs
Emp
ID
1
2
3
0031
0040
0071
Last Name
First
Name
Job
Code
Annual
Salary
GOLDENBERG
WILLIAMS
PERRY
DESIREE
ARLENE M.
ROBERT A.
PILOT
FLTAT
FLTAT
50221.62
23666.12
21957.71
53
Assigning Column Labels
mylib.empdata
EmpID
LastName
FirstName JobCode
0031
GOLDENBERG DESIREE
0040
0071
Salary
PILOT
50221.62
WILLIAMS
ARLENE M. FLTAT
23666.12
PERRY
ROBERT A. FLTAT
21957.71
PROC Step
proc print data=mylib.empdata split=‘/';
label LastName='Last/Name'
FirstName='First/Name'
Salary='Annual/Salary';
title1 'Salary Report';
run;
Salary Report
Obs
Emp
ID
Last
Name
1
2
3
0031
0040
0071
GOLDENBERG
WILLIAMS
PERRY
First
Name
Job
Code
Annual
Salary
DESIREE
ARLENE M.
ROBERT A.
PILOT
FLTAT
FLTAT
50221.62
23666.12
21957.71
54
Exercise
Open C4_Print_ID_BY program, and practice
1. adding two Title statements and one footnote statement to
1st print procedure, and add the variable labels for variables:
Name: Patient Name
ActLevel: Activity Level
Fee: Monthly Fee
2. For the 2nd print statement, change the labels by using SPLIT =
‘/’ in the 2nd print procedure, keep only one title statement and
delete footnote.
Save the program, C4_print_Label, to SASEx folder
55
Answer
PROC SORT data=mylib.admit out=admit_s; by descending ActLevel; run;
proc print data=admit_s label;
by descending actlevel;ID actlevel;
var Name age fee;
sum fee;
label actlevel = 'Activity Level' fee = 'Monthly Fee' Name = 'Patient Name';
title 'print subtotal of Fee sorted by Activity level in ascending order';
title2 'Activity labels for actlevel and fee are defined';
footnote 'Activity level is sorted in descending order';
run;
proc print data=admit_s double split ='/';
by descending actlevel;ID actlevel;
var Name age fee;
sum fee;
label actlevel = 'Activity Level' fee = 'Monthly Fee' Name = 'Patient Name';
title 'print subtotal of Fee with labels';
footnote;
run;
56
Where can you add LABEL statements?
1) Temporary Variable Attributes: LABEL statements can be added
following a PROC PRINT statement. These labels are only
effective for this particular Print statement.
2) Permanent Variable Attributes: LABEL statements can be added
to the DATA step before any PROC PRINT statement. These
LABELS will be in effect for any PROC PRINT statement as long
as the LABEL option is added to the PRINT statement.
NOTE: One can use PROC CONTENTS data =
;
To see the difference between the use of temporary variable
attribute assignment and the permanent variable attribute
assignment.
57
Permanent Label statements
Data emp; set mylib.empdata;
Label lastname = ‘Last*Name’
firstname = ‘First*Name’
salary = ‘Annual*Salary’;
proc print data=emp split=‘*';
title1 'Salary Report';
run;
Labels are created in the DATA step. They can be used in
any of the PROC PRINT procedures following the DATA step
defining variable labels. These labels are permanent labels
for this SAS program.
58
Exercise
C4_print_Label program, and revise the program to
create permanent labels in stead.
NOTE: You need to create a DATA step to read the
Admit data set first, then, sort the new data set created
by the Data Step.
Then, create permanent labels.
Save the program with name c4_print_perm_Label to
your SASEx folder.
59
Answer
data admit; set mylib.admit;
label actlevel = 'Activity Level' fee = 'Monthly Fee' Name = 'Patient Name';
PROC SORT data=admit out=admit_s; by descending ActLevel; run;
proc print data=admit_s label; by descending actlevel;
ID actlevel; var Name age fee;
sum fee;
title 'Permanent Label';
run;
data admit; set mylib.admit;
label actlevel = 'Activity/Level' fee = 'Monthly/Fee' Name = 'Patient/Name';
PROC SORT data=admit out=admit_s; by descending ActLevel; run;
proc print data=admit_s double split ='/'; by descending actlevel;
ID actlevel; var Name age fee; sum fee;
title 'permanent label, double space';
run;
60
Formatting Data Values Using SAS Formats
Enhance the readability of reports by formatting
the data values.
Salary Report
Obs
Emp
ID
Last
Name
1
2
3
4
5
6
7
8
0031
0040
0071
0082
0091
0106
0355
0366
GOLDENBERG
WILLIAMS
PERRY
MCGWIER-WATTS
SCOTT
THACKER
BELL
GLENN
First
Name
DESIREE
ARLENE M.
ROBERT A.
CHRISTINA
HARVEY F.
DAVID S.
THOMAS B.
MARTHA S.
Job
Code
PILOT
FLTAT
FLTAT
PILOT
FLTAT
FLTAT
PILOT
PILOT
Annual
Salary
$50,221.62
$23,666.12
$21,957.71
$96,387.39
$32,278.40
$24,161.14
$59,803.16
$120,202.38
61
Formatting Data Values Using User-defined Formats
•Create custom formats to recode data values in a report.
Salary Report in Categories
Emp
ID
Last
Name
0031
0040
0071
0082
0091
0106
0355
0366
GOLDENBERG
WILLIAMS
PERRY
MCGWIER-WATTS
SCOTT
THACKER
BELL
GLENN
First
Name
JobCode
DESIREE
ARLENE M.
ROBERT A.
CHRISTINA
HARVEY F.
DAVID S.
THOMAS B.
MARTHA S.
Pilot
Flight
Flight
Pilot
Flight
Flight
Pilot
Pilot
Annual
Salary
Attendant
Attendant
Attendant
Attendant
More than
Less than
Less than
More than
25,000 to
Less than
More than
More than
50,000
25,000
25,000
50,000
50,000
25,000
50,000
50,000
62
Formatting Data Values
You can enhance reports by using SAS formats
to format data values in your report.
SAS
Data
Set
Format
Report
Values in the SAS data set are not changed.
The formats are used to display the data values only.
63
Formatting Data Values Using SAS Formats
•To apply a format to a specific variable, use the
FORMAT statement.
•General form of the FORMAT statement:
•
FORMAT variable(s) format;
•Example:
proc print data=mylib.empdata;
format Salary dollar11.2;
run;
NOTE: Using Format statement within a specific PROC
PRINT statement only creates a temporary data value
format assignment.
64
What Is a SAS Format?
•A format is an instruction that SAS uses to write
data values.
•SAS formats have the following form:
<$>format<w>.<d>
Indicates a
character
format
Number of
decimal places
Format name
Required
delimiter
Total width (including
decimal places and
special characters)
65
SAS Formats
If you do not specify a format width large enough to
accommodate a numeric value, the displayed value is
automatically adjusted to fit into the width using so-called
‘BEST’ format defined by SAS
Stored
Value
27134.2864
27134.2864
27134.2864
27134.2864
27134.2864
27134.2864
27134.2864
Format
COMMA12.2
12.2
DOLLAR12.2
DOLLAR9.2
DOLLAR8.2
DOLLAR5.2
DOLLAR4.2
Displayed
Value
27,134.29
27134.29
$27,134.29
$27134.29
27134.29
27134
27E3
66
Formatting Data Values using SAS Formats
EmpID
LastName
FirstName JobCode
0031
GOLDENBERG DESIREE
0040
0071
Salary
PILOT
50221.62
WILLIAMS
ARLENE M. FLTAT
23666.12
PERRY
ROBERT A. FLTAT
21957.71
PROC Step
proc print data=mylib.empdata split=' ';
label LastName='Last Name'
FirstName='First Name'
Salary='Annual Salary';
format Salary dollar11.2;
title1 'Salary Report';
run;
Salary Report
Obs
Emp
ID
Last
Name
1
2
3
0031
0040
0071
GOLDENBERG
WILLIAMS
PERRY
First
Name
Job
Code
DESIREE
ARLENE M.
ROBERT A.
PILOT
FLTAT
FLTAT
Annual
Salary
$50,221.62
$23,666.12
$21,957.71
67
Permanent SAS Format
Similar to Label, one can create a permanent format in the
Data Step for any proc print procedure following the data
step that defines Format statement.
For example,
DATA new;
Set old;
FORMAT Fee dollar8.2;
Run;
Proc print; run;
68
Exercise
Open the C4_print_Label program
Add a FORMAT statement to the 1st PROC Print procedure to define
the SAS format for fee using DOLLAR10.2
Add a format statement to the 2nd proc print procedure to define
the SAS format for fee using COMMA9.2
Add PROC CONTENTS procedure to see the variable attributes.
Save it as C4_print_format to your SASEx folder.
Open C4_print_perm_label program.
Add a FORMAT statement in1st data step
Add a format statement to the 2nd DATA step to define the SAS
format for fee using COMMA9.2
Add PROC CONTENTS procedure to see the variable attributes.
Save it as C4_print_perm_format to SASEx folder
69
Answer
/* Temportary Label and Fromat */
PROC SORT data=mylib.admit out=admit_s; by descending ActLevel; run;
proc print data=admit_s label; by descending actlevel;
ID actlevel; var Name age fee; sum fee;
label actlevel = 'Activity Level' fee = 'Monthly Fee' Name = 'Patient Name';
format fee dollar10.2;
title ‘Temporary label and format';
run;
/*Permanent label and format*/
data admit; set mylib.admit;
label actlevel = 'Activity Level' fee = 'Monthly Fee' Name = 'Patient Name';
format fee dollar10.2;
PROC SORT data=admit out=admit_s; by descending ActLevel; run;
proc print data=admit_s label; by descending actlevel;
ID actlevel; var Name age fee;
sum fee;
title 'Permanent Label and format';
run;
proc contents data=admit_s; run;
70
SAS Formats for displaying date
SAS treats date as a numeric data. a SAS date is stored as the number of
days between 01JAN1960 and the specified date. The SAS date value
stored for 01/01/1960 is 0. Here are some examples:
Actual Date
SAS Stored Date value
01/01/1960
0
01/25/1960
24
12/25/1959
-7
01/01/1961
366
SAS uses various formats to display SAS date values in the report.
MMDDYYw.
Format
MMDDYY6.
MMDDYY8.
MMDDYY10.
•Selected SAS date formats:
Displayed
Value
101601
10/16/01
10/16/2001
DATEw.
Format
DATE7.
DATE9.
Displayed
Value
16OCT01
16OCT2001
71
Displaying Date Values using SAS Date Formats
Examples: Date values displayed using different formats:
Stored
Value
Format
Displayed Value
in the report
0
MMDDYY8.
0
MMDDYY10.
1
DATE9.
01/01/60
01/01/1960
02JAN1960
-1
WORDDATE.
December 31, 1959
365
DDMMYY10.
31/12/1960
366
WEEKDATE.
Sunday, January 1, 1961
72
Creating Permanent Data Value Formats
(Permanent Data Value Format assignment)
•Similar to the LABEL statement, SAS also provides
the FORMAT procedure, which enables you to define
custom format as a PERMENANT value label
assignment, defined before PROC PRINT statements,.
•To create and use your own formats,
1. use the FORMAT procedure to create the format
2. apply the format to specific variable(s)
by using a FORMAT statement.
73
Creating Permanent SAS Data Value Formats
Data emp; set mylib.empdata;
Format salary dollar12.2;
Label lastname = ‘Last*Name’
firstname = ‘First*Name’
salary = ‘Annual*Salary’;
proc print data=emp split=‘*';
title1 'Salary Report';
run;
The output will display variable labels for lartname, firstname
and salary as defined in the Label statement, and Salary in
dollar12.2 format
74
Example of using temporary labels and temporary
Data Value format for Date using DATE9.
DATA admit1; set mylib.admit;
Run;
Proc print data = admit1 split =‘ ‘;
Var Sex Age date Actlevel fee;
ID Name;
Label sex =‘Gender’
actlevel=‘Activity Level’;
Format date date9.;
Run;
75
Example of using temporary labels and temporary
SAS Date format MMDDYY10. for variable Date
DATA admit1; set mylib.admit;
Run;
Proc print data = admit1 split =‘ ‘;
Var Sex Age date Actlevel fee;
ID Name;
Label sex =‘Gender’
actlevel=‘Activity Level’;
Format date MMDDYY10.;
Run;
76
Example of using permanent labels
and permanent SAS format for Date
DATA admit1; set mylib.admit;
Label sex =‘Gender’
actlevel=‘Activity Level’;
Format date date9.;
Run;
Proc print data= admit1 split =‘ ‘;
Var Sex Age date Actlevel fee;
ID Name;
Run;
77
Exercise
In the Admit data set, there is a variable ‘Date’, the date for the admission.
ID
Name
Sex
Age
Date
Height
Weight
ActLevel
Fee
2458
Murray, W
M
27
1
72
168
HIGH
85.20
2462
Almers, C
F
34
3
66
172
HIGH
124.80
2501
Bonaventure, T
F
31
17
61
123
LOW
149.75
The data value for Date is numeric, counted from 1/1/1960 with 1/1/1960 to be
zero.
Thus, Date =1 is 1/2/1960, Date = 3 is 1/4/1960
However, when we print the data set, it shows as values 1,3, etc., not 1/2/1960,
1/4/1960 etc. In order to display the Date properly as 4JAN1960 or 1/4/1960SAS
provides SAS formats to display Date as described in the previous slices.
Write a program to print the Admit data set and use the SAS date format ,
DATE9. to display the variable Date.
Add anther PROC procedure to print the Admit data set and use the SAS date
format ,MMDDYY10. to display the variable Date.
Add a new data set Admitn by inout Admit from Mylib, and create a permanent
SAS format for displaying Date using the SAS format Date9., then print the
Admitn
78
Answer
proc print data=mylib.admit;
format date date9.;
run;
proc print data=mylib.admit;
format date mmddyy10.;
run;
/*create a permanent SAS format for date using DATE9. */
data admitn; set mylib.admit;
format date date9.;
proc print data=admitn;
run;
79
Download