| Examples of SAS/ACCESS DATA Step Programs |
This example illustrates how a program can reestablish currency on a record to complete set navigation after accessing a record that is not contained in the current set occurrence.
In this example, a transaction SAS data set, WORK.EMPLOYEE, supplies a CALC key value for the OBTAIN of an EMPLOYEE record. COVERAGE records are then obtained within the current EMP-COVERAGE set occurrence. PLANCODE values from employee COVERAGE records provide links to INSURANCE-PLAN records through a CALC key. Once current on INSURANCE-PLAN, the program gathers data and uses a stored database key to return to the current COVERAGE record. At that point, the next COVERAGE record in the current set occurrence of EMP-COVERAGE can be obtained. The output data set consists of observations which contain employee, coverage, and related insurance plan data. The numbers in the program correspond to the numbered comments following the program.
1 *options $idmdbug; 2 data work.employee; input empnum $4.; datalines; 0007 0471 0000 0301 0004 ; data work.empplan; drop covdbkey empnum; 3 infile empss01 idms func=func record=recname ikey=ckey ikeylen=keyl errstat=stat sequence=seq set=inset area=subarea dbkey=dkey; /* BIND records to be accessed */ 4 if _n_ = 1 then do; func = 'BIND'; recname = 'EMPLOYEE'; input; if stat ne '0000' then go to staterr; recname = 'INSURANCE-PLAN'; input; if stat ne '0000' then go to staterr; recname = 'COVERAGE ; input; if stat ne '0000' then go to staterr; end; /* OBTAIN EMPLOYEE record using CALC key */ /* value */ 5 set work.employee; func = 'OBTAIN'; seq = ' '; inset = ' '; ckey = empnum; keyl = 4; recname = 'EMPLOYEE'; input @; if stat not in ('0000', '0326') then go to staterr; if stat = '0000' then do; input @1 employee_id 4.0 @5 firstname $char10. @15 lastname $char15.; /* OBTAIN COVERAGE records for EMPLOYEE */ 6 seq = 'FIRST'; do while (stat = '0000'); func = 'OBTAIN'; keyl = 0; ckey = ' '; dkey = ' '; recname = 'COVERAGE'; inset = 'EMP-COVERAGE'; input @; if stat not in ('0000', '0307') then go to staterr; if stat = '0000' then do; input @13 type $1. @14 plancode $3.; /* ACCEPT CURRENT database key */ 7 func = 'ACCEPT'; seq = 'CURRENT'; dkey = ' '; input; if stat ne '0000' then go to staterr; covdbkey = dkey; /* FIND INSURANCE-PLAN using CALC */ 8 func = 'FIND'; ckey = plancode; keyl = 3; seq = ' '; recname = 'INSURANCE-PLAN'; inset = ' '; dkey = ' '; input; if stat ne '0000' then go to staterr; /* OBTAIN CURRENT INSURANCE-PLAN */ /* record */ 9 func = 'OBTAIN'; seq = 'CURRENT'; ckey = ' '; keyl = 0; recname = ' '; subarea = ' '; input @; if stat ne '0000' then go to staterr; input @4 company_name $45. @105 group_number 6.0 @111 plndeduc PD5.2 @116 maxlfcst PD5.2 @121 famlycst PD5.2 @126 depcost PD5.2; output; /* FIND COVERAGE using stored */ /* database key */ 10 func = 'FIND'; seq = ' '; recname = 'COVERAGE'; dkey = covdbkey; input; if stat ne '0000' then go to staterr; seq = 'NEXT'; end; end; end; 11 else do; put 'WARNING: No EMPLOYEE record for CALC= 'ckey; put 'WARNING: Execution continues with next EMPLOYEE.'; _error_ = 0; end; 12 _error_ = 0; return; 13 staterr: put @1 'ERROR: ' @10 func @17 'RETURNED STATUS =' @37 stat; put @1 'ERROR: INFILE parameter values are: '; put @1 'ERROR: ' recname= ckey= keyl= seq= inset= subarea= dkey=; put @1 'ERROR: DATA step execution terminating.'; _error_ = 0; stop; run; proc print data=work.empplan; title 'Employee Coverage and Plan Record Information'; run;
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
| |
|
|
The following output shows a portion of the output from this program.
Reestablishing Currency on a Record
Employee Coverage and Plan Record Information
employee_
Obs id firstname lastname type plancode
1 7 MONTE BANK F 004
2 471 THEMIS PAPAZEUS F 003
3 471 THEMIS PAPAZEUS F 002
4 471 THEMIS PAPAZEUS M 001
5 301 BURT LANCHESTER D 004
6 301 BURT LANCHESTER F 003
7 301 BURT LANCHESTER F 002
8 301 BURT LANCHESTER M 001
9 4 HERBERT CRANE F 004
10 4 HERBERT CRANE F 003
11 4 HERBERT CRANE M 001
group_
Obs company_name number
1 TEETH R US 545598
2 HOLISTIC GROUP HEALTH ASSOCIATION 329471
3 HOMOSTASIS HEALTH MAINTENANCE PROGRAM 952867
4 PROVIDENTIAL LIFE INSURANCE 347815
5 TEETH R US 545598
6 HOLISTIC GROUP HEALTH ASSOCIATION 329471
7 HOMOSTASIS HEALTH MAINTENANCE PROGRAM 952867
8 PROVIDENTIAL LIFE INSURANCE 347815
9 TEETH R US 545598
10 HOLISTIC GROUP HEALTH ASSOCIATION 329471
11 PROVIDENTIAL LIFE INSURANCE 347815
Obs plndeduc maxlfcst famlycst depcost
1 50 0 5000 1000
2 200 0 200 200
3 0 0 900000 100000
4 0 100000 0 0
5 50 0 5000 1000
6 200 0 200 200
7 0 0 900000 100000
8 0 100000 0 0
9 50 0 5000 1000
10 200 0 200 200
11 0 100000 0 0
Copyright © 2007 by SAS Institute Inc., Cary, NC, USA. All rights reserved.