<strong>PROC PRINT</strong>
is a type of procedure that is used by every SAS programmer frequently. Most of you must be using it just to display your data, but you can also use it to generate pretty formatted outputs.
Below are some things you can do with PRINT that may make even this standard procedure extra helpful.
1. Using the ID and BY Statements in PROC PRINT
You can use the ID
and BY
statements together. Although the PROC PRINT procedure doesn’t have a CLASS statement, you possibly can offset groups with a combination of the BY
and ID
statements.
Variables common to these two statements will group the other variables and insert a blank line after each group.
proc sort data=sashelp.class out=class2;
by sex;
run;
proc print data=class2;
by sex;
id sex;
var name age weight;
run;
- The BY and ID statements use the variable SEX in the PRINT step. When used together in this manner, the value for SEX is written only once for each SEX. This is also the default behaviour for PROC REPORT for GROUP and ORDER variables.
- A blank line has been inserted after each SEX.
Note: The variables used in the BY statements must be sorted.
2. Using the STYLE= Option with PROC PRINT
The STYLE=
option is used to alter the attributes generated by the selected ODS style.
Below is the general syntax, and curly brackets surround the attributes.
style<(location)>={<em>attribute=attribute_value</em>}
In the current releases of SAS, you’ll be able to use the square bracket as an alternative to the curly braces. Specifying the location is non-obligatory since there’s a default assignment when it’s left off.
However, you usually will want to specify the location as it is used to manage the attribute assignment’s place. The supported locations that you can include are:
- DATA cells (also COLUMNS or COL)
- TOTAL sub-total (used on the SUM statement)
- GRANDTOTAL overall total (used on the SUM statement)
- HEADER column header (also HEAD and HDR)
- N used when the N option is specified
- OBS cells in the OBS column
- OBSHEADER header for the OBS column
- TABLE controls table structure resembling cell width
You can apply the STYLE= option to the PROC PRINT statement and on other proc step statements. A combination of the desired location and the statement containing the option will decide what portion of the table is modified by the option.
Not all style option locations are applicable for all PRINT statements. The following table reveals the available locations and the statements to which they are often applied.
PROC STATEMENTS | Supported Style Locations |
PROC PRINT | data header, n, obs, obsheader, table |
BY | none |
ID | header, data |
VAR | header, data |
SUM | header, data, total, grandtotal |
You can modify some of the style attributes like-
- BACKGROUND
- BORDERCOLOR
- BORDERCOLORDARK
- BORDERCOLORLIGHT
- FONT_FACE
- FONT_WEIGHT
- FOREGROUND
Other Style Options
Although style attributes used with the PROC statement usually apply to the table as a whole, STYLE= options applied to supporting statements can provide extra control in the styling.
For example, you may want the first column to be of different background colour. Additionally, these attributes will likely override these settings on the PROC statement.
This lets you specify completely different options for different variables.
proc print data=sashelp.cars (obs=10) noobs label
style(col)=[backgroundcolor=lightcyan]
style(header)= [background=yellow font_weight=bold]
style(obs)= [background=lightred]
n='Count of Model = '
;
var origin/style(header)=[backgroundcolor=lightcoral]
style(column)=[backgroundcolor=lightpink foreground=blue];
var origin Make Model MSRP;
sum MSRP/style(grandtotal)=[backgroundcolor=darkgreen foreground=white];
title1 "Sum of MSRP";
footnote1 "Note: Number of Records are limited to 10";
footnote2 "DATASET: SASHELP.CARS";
run;
Note the NOBS option is used to eliminate the OBS column. The LABEL
allows column labels that are not just variable names.
The SUM statement allows for a summary of numbers to be produced in the listing. It is useful when subgroup and total summaries need to be generated, while the N
option prints the number of observations.
Two locations(header, column) are specified for the ORIGIN variable.
The total receives two attribute overrides- backgroundcolor and foreground.
3. Using PROC PRINT to Generate a Table of Contents
The PRINT procedure is designed to work with lists of data. When the list of values
contains hyperlinks to different files, PRINT can create a table of contents for a report.
In this instance, HTML anchor tags are formed as data values. When they are displayed by ODS in the HTML destination, these values change into hyperlinks to different files.
The character variable CLINIC is used to hold the HTML anchor tag. The CATT function n is used as an anchor tag statement.
The clinic number o is used in the name of the clinic p will be step q. All that’s required is that the destination doesn’t know what to do with an HTML anchor tag and can subsequently show the data as it’s stored.
When the table is displayed using the HTML destination, the value is interpreted as an anchor tag and a linkable item. Here the first four items in the PROC PRINT are shown.
data Links;
length websites $70;
input websites;
datalines;
Google
Facebook
Apple
Microsoft
;
run;
data toc;
set links;
weblinks=catt("",websites,"");
run;
ods html body=toc.html;
proc print data=toc;
run;
ods html close;
4. Generating Column Totals
To produce column totals for numeric variables, you can list the variables to be summed in a SUM statement in your PROC PRINT step.
proc print data=cert.insure;
var name policy balancedue;
where pctinsured < 100;
sum balancedue;
run;
Column totals appear at the end of the report in the same format as the values of the variables.