Suppose you have
the following items:
-
a view descriptor, Vlib.CustAcct, that is based on the CUSTOMER and CHCKACCT segments
of the IMS database AcctDBD.
-
a SAS data file, MyData.ChgData, which contains checking account numbers and checking
fees.
-
a view descriptor, MyData.BankChrg, that is based on data in a DB2 table that contains
additional banking
fees. (The MyData.BankChrg view descriptor has been created using the SAS/ACCESS interface
to DB2.)
You can use PROC SQL to create a view that joins all these sources of data. When
you use the
PROC SQL view in your SAS program, the joined data is presented in a single output table. In this
example, using the SAS WHERE or subsetting IF statements would not be an appropriate
way of presenting data from various sources because you want to compare variables
from several sources rather than simply merge or concatenate the data. For more information
about the DB2 table that is used in this example, see
Example Data.
CAUTION:
When you
use PROC SQL to access more than one IMS database, the view descriptors
for each database must use the same PSB.
In addition, a PCB must be included in that PSB for each database that you want to
access. If you are accessing the same database multiple times,
each view descriptor must specify a different PCB using the PCB index
field.
The following code prints the view descriptors and the SAS data file:
options nodate linesize=120;
proc print data=vlib.custacct;
title2 'Data Described by VLIB.CUSTACCT';
run;
options nodate linesize=80;
proc print data=mydata.bankchrg;
title2 'Data Described by MYDATA.BANKCHRG';
run;
proc print data=mydata.chgdata;
title2 'SAS Data File MYDATA.CHGDATA';
run;
The following three outputs show the results of the PRINT procedure performed on the
Vlib.CustAcct view descriptor (based on IMS data), the MyData.BankChrg view descriptor
(based on DB2 data), and
the MyData.ChgData data file.
Data That Is Described by Vlib.CustAcct
The SAS System
Data Described by VLIB.CUSTACCT
SOC_SEC_ CHECK_ACCOUNT_
OBS NUMBER CUSTOMER_NAME NUMBER
1 667-73-8275 WALLS, HOOPER J. 345620145345
2 667-73-8275 WALLS, HOOPER J. 345620154633
3 434-62-1234 SUMMERS, MARY T. 345620104732
4 436-42-6394 BOOKER, APRIL M. 345620135872
5 434-62-1224 SMITH, JAMES MARTIN 345620134564
6 434-62-1224 SMITH, JAMES MARTIN 345620134663
7 178-42-6534 PATTILLO, RODRIGUES 745920057114
8 156-45-5672 O'CONNOR, JOSEPH 345620123456
9 657-34-3245 BARNHARDT, PAMELA S. 345620131455
10 667-82-8275 COHEN, ABRAHAM 382957492811
11 456-45-3462 LITTLE, NANCY M. 345620134522
12 234-74-4612 WIKOWSKI, JONATHAN S.
345620113263
Data That Is Described by MyData.BankChrg
The SAS System
Data Described by MYDATA.BANKCHRG
OBS ssn accountn chckchrg atmfee loanchrg
1 667-73-8275 345620145345 3.75 5.00 2.00
2 434-62-1234 345620104732 15.00 25.00 552.23
3 436-42-6394 345620135872 1.50 7.50 332.15
4 434-62-1224 345620134564 9.50 0.00 0.00
5 178-42-6534 . 0.50 15.00 223.77
6 156-45-5672 345620123456 0.00 0.00 0.00
7 657-34-3245 345620132455 10.25 10.00 100.00
8 667-82-8275 . 7.50 7.50 175.75
9 456-45-3462 345620134522 23.00 30.00 673.23
10 234-74-4612 345620113262 4.50 7.00 0.00
Data in the SAS Data File MyData.ChgData
The SAS System
SAS Data File MYDATA.CHGDATA
OBS account charge
1 345620135872 $10
2 345620134522 $7
3 345620123456 $12
4 382957492811 $3
5 345620134663 $8
6 345620131455 $6
7 345620104732 $9
The following SAS statements select and combine data from these three sources to
create a PROC SQL view, SQL.CHARGES. The SQL.CHARGES view retrieves checking fee
information so that the bank
can charge customers for checking services.
options nodate linesize=132;
libname sql 'SAS-data-library';
proc sql;
create view sql.charges as
select distinct custacct.soc_sec_number,
custacct.customer_name,
custacct.check_account_number,
chgdata.charge,
bankchrg.chckchrg,
bankchrg.atmfee,
bankchrg.loanchrg
from vlib.custacct,
mydata.bankchrg,
mydata.chgdata
where custacct.soc_sec_number=bankchrg.ssn and
custacct.check_account_number=chgdata.account;
title2 'Banking Charges for the Month';
select * from sql.charges;
The CREATE statement incorporates a WHERE clause along with the SELECT clause. The
last SELECT statement retrieves and displays the PROC SQL view SQL.CHARGES. To select
all the items from the view, use an asterisk (*) in place of
item names. When an asterisk is used, the order of the items displayed matches the
order of the items as specified in the SQL.CHARGES view definition. Notice that PROC
SQL prints the output automatically to the display using the IMS item names instead
of the SAS variable names. It also executes without a RUN statement when the procedure
is submitted.
The following output shows the data that is described by the PROC SQL view SQL.CHARGES.
Results of Combining Data from Various Sources
The SAS System
Banking Charges for the Month
SOC_SEC_ CHECK_ACCOUNT_
NUMBER CUSTOMER_NAME NUMBER charge chckchrg atmfee loanchrg
----------------------------------------------------------------------------------------
156-45-5672 O'CONNOR, JOSEPH 345620123456 $12 0.00 0.00 0.00
434-62-1224 SMITH, JAMES MARTIN 345620134663 $8 9.50 0.00 0.00
434-62-1234 SUMMERS, MARY T. 345620104732 $9 15.00 25.00 552.23
436-42-6394 BOOKER, APRIL M. 345620135872 $10 1.50 7.50 332.15
456-45-3462 LITTLE, NANCY M. 345620134522 $7 23.00 30.00 673.23
657-34-3245 BARNHARDT, PAMELA S. 345620131455 $6 10.25 10.00 100.00
667-82-8275 COHEN, ABRAHAM 382957492811 $3 7.50 7.50 175.75