O'Reilly logo

Step-by-Step Programming with Base SAS 9.4, Second Edition, 2nd Edition by SAS Institute

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

If you are updating a data set, you probably do not want duplicate values of BY variables
in the master data set. For example, if you update by Name, each observation in the
master data set should have a unique value of Name. If you update by Name and Age,
two or more observations can have the same value for either Name or Age but should
not have the same values for both. SAS warns you if it finds duplicates but proceeds
with the update. It applies all transactions only to the first observation in the BY group in
the master data set.
Updating a Data Set
In this example, the circulation department of a magazine maintains a mailing list that
contains tens of thousands of names. Each issue of the magazine contains a form for
readers to fill out when they change their names or addresses. To simplify the
maintenance job, the form requests that readers send only new information. New
subscribers can start a subscription by completing the entire form. When a form is
received, a data entry operator enters the information about the form into a raw data file.
The mailing list is updated once per month from the raw data file.
The mailing list includes these variables for each subscriber:
SubscriberId
specifies a unique number that is assigned to the subscriber at the time the
subscription begins. A subscriber's SubscriberId never changes.
Name
specifies the subscriber's name. The last name appears first, followed by a comma
and the first name.
StreetAddress
specifies the subscriber's street address.
City
specifies the subscriber's city.
StateProv
specifies the subscriber's state or province. This variable is missing for addresses
outside the United States and Canada.
PostalCode
specifies the subscriber's postal code (ZIP code for addresses in the United States).
Country
specifies the subscriber's country.
The following program creates and displays the first part of this data set. The raw data
are already sorted by SubscriberId.
data mail_list;
input SubscriberId 1-8 Name $ 9-27 StreetAddress $ 28-47 City $ 48-62
StateProv $ 63-64 PostalCode $ 67-73 Country $ ;
datalines;
1001 Ericson, Jane 111 Clancey Court Chapel Hill NC 27514 USA
1002 Dix, Martin 4 Shepherd St. Vancouver BC V6C 3E8 Canada
1003 Gabrielli, Theresa Via Pisanelli, 25 Roma 00196 Italy
1004 Clayton, Aria 14 Bridge St. San Francisco CA 94124 USA
1005 Archuleta, Ruby Box 108 Milagro NM 87429 USA
1006 Misiewicz, Jeremy 43-C Lakeview Apts. Madison WI 53704 USA
Updating a Data Set 319
1007 Ahmadi, Hafez 52 Rue Marston Paris 75019 France
1008 Jacobson, Becky 1 Lincoln St. Tallahassee FL 32312 USA
1009 An, Ing 95 Willow Dr. Toronto ON M5J 2T3 Canada
1010 Slater, Emily 1009 Cherry St. York PA 17407 USA
;
run;
proc print data=mail_list;
title 'Magazine Master Mailing List';
run;
The following output displays the master MAIL_LIST data set:
Figure 20.1 Magazine Master Mailing List
This month the information that follows is received for updating the mailing list:
Martin Dix changed his name to Martin Dix-Rosen.
Jane Ericson's postal code changed.
Jeremy Misiewicz moved to a new street address. His city, state, and postal code
remain the same.
Ing An moved from Toronto, Ontario, to Calgary, Alberta.
Martin Dix-Rosen, shortly after changing his name, moved from Vancouver, British
Columbia, to Seattle, Washington.
Two new subscribers joined the list. They are given SubscriberID numbers 1011 and
1012.
Each change is entered into the raw data file as soon as it is received. In each case, only
the customer's SubscriberId and the new information are entered. The raw data file looks
like this:
1002 Dix-Rosen, Martin
1001 27516
1006 932 Webster St.
1009 2540 Pleasant St. Calgary AB T2P 4H2
1011 Mitchell, Wayne 28 Morningside Dr. New York NY 10017 USA
320 Chapter 20 Updating SAS Data Sets

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required