![]() |
![]() |
Examples of SAS/ACCESS DATA Step Programs |
This example shows how to navigate multiple set relationships and use direct access methods involving database record keys. The output consists of observations containing related employee, office, and dental claim information. Observations are only output for employees that have dental claim record occurrences. To gather the information, the program performs an area sweep for the DEPARTMENT records and uses the FIND command to establish currency and navigate the DEPT-EMPLOYEE, OFFICE-EMPLOYEE, EMP-COVERAGE, and COVERAGE-CLAIMS sets. By accepting and storing database keys, currency can be re-established on the EMPLOYEE record after obtaining OFFICE information and before gathering COVERAGE and DENTAL CLAIM information. The numbers in the program correspond to the numbered comments following the program.
1 *options $idmdbug; data work.dental_records; drop tempkey; 2 infile empss01 idms func=func record=recname dbkey=dkey errstat=stat sequence=seq set=inset area=subarea; /* BIND the records to be accessed */ 3 if _n_ = 1 then do; func = 'BIND'; recname = 'EMPLOYEE'; input; if stat ne '0000' then go to staterr; recname = 'DEPARTMENT'; input; if stat ne '0000' then go to staterr; recname = 'COVERAGE'; input; if stat ne '0000' then go to staterr; recname = 'DENTAL-CLAIM'; input; if stat ne '0000' then go to staterr; recname = 'OFFICE'; input; if stat ne '0000' then go to staterr; end; /* FIND FIRST/NEXT DEPARTMENT record in */ /* area ORG-DEMO-REGION */ 4 seq = 'NEXT'; if _n_ = 1 then seq = 'FIRST'; func = 'FIND'; recname = 'DEPARTMENT'; subarea = 'ORG-DEMO-REGION'; inset = ' '; input; if stat not in ('0000', '0307') then go to staterr; /* STOP DATA step execution if no more */ /* DEPARTMENT records */ 5 if stat = '0307' then do; _error_ = 0; stop; end; 6 do until (stat ne '0000'); /* OBTAIN NEXT EMPLOYEE record */ func = 'OBTAIN'; seq = 'NEXT'; recname = 'EMPLOYEE'; inset = 'DEPT-EMPLOYEE'; input @; if stat not in ('0000','0307') then go to staterr; if stat = '0000' then do; input @1 employee_id 4.0 @5 firstname $char10. @15 lastname $char15. @30 street $char20. @50 city $char15. @65 state $char2. @67 zip $char9. @76 phone 10.0 @86 status $char2. @88 ssnumber $char9. @109 birthdate yymmdd6.; /* ACCEPT DBKEY for current EMPLOYEE and */ /* store in tempkey */ 7 func = 'ACCEPT'; seq = 'CURRENT'; dkey = ' '; inset = ' '; input; if stat ne '0000' then go to staterr; tempkey=dkey; /* OBTAIN OFFICE record for current */ /* EMPLOYEE */ 8 func = 'OBTAIN'; seq = 'OWNER'; dkey = ' '; inset = 'OFFICE-EMPLOYEE'; input @; if stat ne '0000' then go to staterr; input @1 office_code $char3. @4 office_street $char20. @24 office_city $char15. @39 office_state $char2. @41 office_zip $char9.; /* FIND EMPLOYEE using DBKEY stored in */ /* tempkey */ 9 func = 'FIND'; recname = ' '; dkey = tempkey; seq = ' '; inset = ' '; input; if stat ne '0000' then go to staterr; /* FIND FIRST COVERAGE record for */ /* current EMPLOYEE */ 10 func = 'FIND'; recname = 'COVERAGE'; dkey = ' '; seq = 'FIRST'; inset = 'EMP-COVERAGE'; input; if stat ne '0000' then go to staterr; /* OBTAIN LAST DENTAL-CLAIM record */ /* within COVERAGE-CLAIMS */ /* Observations are only OUTPUT for */ /* employees with dental claim records */ 11 func = 'OBTAIN'; recname = 'DENTAL-CLAIM'; seq = 'LAST'; inset = 'COVERAGE-CLAIMS'; input @; if stat not in ('0000','0307') then go to staterr; do while (stat eq '0000'); input @1 claim_year $2. @3 claim_month $2. @5 claim_day $2. @7 claim_firstname $10. @17 claim_lastname $15. @32 birthyear $2. @34 birthmonth $2. @36 birthday $2. @38 sex $1. @39 relation $10. @49 dds_firstname $10. @59 dds_lastname $15. @74 ddsstreet $20. @94 ddscity $15. @109 ddsstate $2. @111 ddszip $9. @120 license $6. @126 num_procedure ib2. @131 tooth_number $2. @133 service_year $2. @135 service_month $2. @137 service_day $2. @139 procedure_code $4. @143 descservice $60. @203 fee pd5.2; output; /* OBTAIN PRIOR DENTAL-CLAIM record */ seq = 'PRIOR'; input @; end; /* When DENTAL-CLAIM records have been */ /* processed, release INPUT buffer and */ /* reset STAT to OBTAIN NEXT EMPLOYEE */ 12 if stat = '0307' then do; stat = '0000'; input; end; else go to staterr; end; end; /* When all EMPLOYEEs have been processed, */ /* reset ERROR flag and continue with next */ /* DEPARTMENT */ 13 _error_ = 0; return; 14 STATERR: put @1 'ERROR: ' @10 func @17 'RETURNED STATUS =' @37 stat; put @1 'ERROR: INFILE parameter values are: '; put @1 'ERROR: ' recname= seq= inset= dkey= subarea=; put @1 'ERROR: DATA step execution terminating.'; _error_ = 0; stop; run; proc print data=work.dental_records; format birthdate date9.; title1 'Dental Claim Information'; run;
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
The following output shows a portion of the output from this program.
Navigating Multiple Set Relationships
Dental Claim Information employee_ Obs id firstname lastname street city state zip 1 4 HERBERT CRANE 30 HERON AVE KINGSTON NJ 21341 2 30 HENRIETTA HENDON 16 HENDON DR WELLESLEY MA 02198 office_ Obs phone status ssnumber birthdate code office_street 1 2013341433 01 016777451 420321 001 20 W BLOOMFIELD ST 2 6178881212 01 011334444 331006 002 567 BOYLSTON ST office_ office_ claim_ claim_ claim_ claim_ claim_ Obs office_city state zip year month day firstname lastname 1 SPRINGFIELD MA 02076 80 10 04 JESSICA CRANE 2 BOSTON MA 02243 77 05 23 HELOISE HENDON dds_ dds_ Obs birthyear birthmonth birthday sex relation firstname lastname 1 57 01 11 F WIFE DR PEPPER 2 68 03 15 F DAUGHTER SAL SARDONICUS num_ tooth_ Obs ddsstreet ddscity ddsstate ddszip license procedure number 1 78 COLA RD PRINCETON NJ 01762 877073 2 08 2 402 NATURE'S WAY NEEDHAM MA 02243 459631 1 14 service_ service_ service_ procedure_ Obs year month day code descservice fee 1 80 09 16 0076 FILLING 14 2 77 05 02 0076 FILLING 14
![]() |
![]() |
Copyright © 2007 by SAS Institute Inc., Cary, NC, USA. All rights reserved.