Prerequisites
Before continuing, you should understand the concepts presented in the following
sections:
Chapter 1, “What is the SAS System?,” on page 3
Chapter 3, “Introduction to DATA Step Processing,” on page 27
Understanding the Basics
When you use a SAS data set as input into a DATA step, the description of the data set is
available to SAS. In your DATA step, use a SET, MERGE, MODIFY, or UPDATE
statement to read the SAS data set. Use SAS programming statements to process the data
and create an output SAS data set.
In a DATA step, you can create a new data set that is a subset of the original data set. For
example, if you have a large data set of personnel data, you might want to look at a
subset of observations that meet certain conditions, such as observations for employees
hired after a certain date. Alternatively, you might want to see all observations but only a
few variables, such as the number of years of education or years of service to the
company.
When you use existing SAS data sets, as well as with subsets created from SAS data
sets, you can make more efficient use of computer resources than if you use raw data or
if you are working with large data sets. Reading fewer variables means that SAS creates
a smaller program data vector, and reading fewer observations means that fewer
iterations of the DATA step occur. Reading data directly from a SAS data set is more
efficient than reading the raw data again, because the work of describing and converting
the data has already been done.
One way of looking at a SAS data set is to produce a listing of the data in a SAS data set
by using the PRINT procedure. Another way to look at a SAS data set is to display
information that describes its structure rather than its data values. To display information
about the structure of a data set, use the DATASETS procedure with the CONTENTS
statement. If you need to work with a SAS data set that is unfamiliar to you, the
CONTENTS statement in the DATASETS procedure displays information such as the
name, type, and length of all the variables in the data set. An example that shows the
CONTENTS statement in the DATASETS procedure is shown in “Input SAS Data Set
for Examples” on page 92.
Input SAS Data Set for Examples
The examples in this section use a SAS data set named CITY, which contains
information about expenditures for a small city. It reports total city expenditures for the
years 1980 through 2000 and divides the expenses into two major categories: services
and administration.
The following example uses the DATASETS procedure with the NOLIST option to
display the CITY data set. The NOLIST option prevents the DATASETS procedure from
listing other data sets that are also located in the WORK library:
92 Chapter 6 Starting with SAS Data Sets
data city;
input Year 4. @7 ServicesPolice comma6.
@15 ServicesFire comma6. @22 ServicesWater_Sewer comma6.
@30 AdminLabor comma6. @39 AdminSupplies comma6.
@45 AdminUtilities comma6.;
ServicesTotal=ServicesPolice+ServicesFire+ServicesWater_Sewer;
AdminTotal=AdminLabor+AdminSupplies+AdminUtilities;
Total=ServicesTotal+AdminTotal;
label Total='Total Outlays'
ServicesTotal='Services: Total'
ServicesPolice='Services: Police'
ServicesFire='Services: Fire'
ServicesWater_Sewer='Services: Water & Sewer'
AdminTotal='Administration: Total'
AdminLabor='Administration: Labor'
AdminSupplies='Administration: Supplies'
AdminUtilities='Administration: Utilities';
datalines;
1993 2,819 1,120 422 391 63 98
1994 2,477 1,160 500 172 47 70
1995 2,028 1,061 510 269 29 79
1996 2,754 893 540 227 21 67
1997 2,195 963 541 214 21 59
1998 1,877 926 535 198 16 80
1999 1,727 1,111 535 213 27 70
2000 1,532 1,220 519 195 11 69
2001 1,448 1,156 577 225 12 58
2002 1,500 1,076 606 235 19 62
2003 1,934 969 646 266 11 63
2004 2,195 1,002 643 256 24 55
2005 2,204 964 692 256 28 70
2006 2,175 1,144 735 241 19 83
2007 2,556 1,341 813 238 25 97
2008 2,026 1,380 868 226 24 97
2009 2,526 1,454 946 317 13 89
2010 2,027 1,486 1,043 226 . 82
2011 2,037 1,667 1,152 244 20 88
2012 2,852 1,834 1,318 270 23 74
2013 2,787 1,701 1,317 307 26 66
;
proc datasets library=work nolist;
contents data=city;
run;
Input SAS Data Set for Examples 93
The following outputs display the contents of the CITY data set, as well as information
about the data set.
Figure 6.1 Part 1: The Structure of CITY as Shown by PROC DATASETS
Figure 6.2 Part 2: The Structure of CITY as Shown by PROC DATASETS
94 Chapter 6 Starting with SAS Data Sets

Get Step-by-Step Programming with Base SAS 9.4, Second Edition, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.