![]() |
![]() |
Examples of SAS/ACCESS DATA Step Programs |
This example, like the previous example, uses the information stored in a SAS data set to locate records in the CA-IDMS database. In this case, not only do the observations in the transaction data set WORK.OFFICE provide CALC information for the OFFICE record, they supply sort key information as well for the EMPLOYEE record. Therefore, the program uses both pieces of information to locate a specific occurrence of the OFFICE record, followed by a specific occurrence of the EMPLOYEE record in the OFFICE-EMPLOYEE set occurrence. If any of the transaction information is incorrect, a WARNING message is issued and no observation is output to WORK.EMP. The numbers in the program correspond to the numbered comments following the program.
1 *options $idmdbug; 2 data work.office; input offkey $3. emp $25.; datalines; 001GARFIELD JENNIFER 002BLOOMER JUNE 005JOE SMITH 008WAGNER RICHARD 010ANDALE ROY ; data work.emp; drop offkey emp; 3 infile empss01 idms func=func record=recname ikey=ckey ikeylen=keyl errstat=stat sequence=seq set=inset sortfld=skey; /* BIND the records to be accessed */ 4 if _n_ = 1 then do; func = 'BIND'; recname = 'EMPLOYEE'; input; if stat ne '0000' then go to staterr; recname = 'OFFICE'; input; if stat ne '0000' then go to staterr; end; /* OBTAIN OFFICE record based on CALC key */ 5 set work.office; func = 'OBTAIN'; ckey = offkey; keyl = 3; recname = 'OFFICE'; inset = ' '; skey = ' '; input @; if stat not in ('0000', '0326') then go to staterr; if stat = '0000' then do; input @1 office_code $char3. @4 office_street $char20. @24 office_city $char15. @39 office_state $char2. @41 office_zip $char9. @50 officephone1 9.0 @59 officephone2 9.0 @68 officephone3 9.0 @77 areacode $char3. @80 speeddial $char3.; /* FIND EMPLOYEE record within set */ /* using SORT key */ 6 func = 'FIND'; skey = emp; ckey = ' '; keyl = 25; recname = 'EMPLOYEE'; inset = 'OFFICE-EMPLOYEE '; input; if stat not in ('0000', '0326') then go to staterr; if stat = '0000' then do; /* OBTAIN CURRENT record */ 7 func = 'OBTAIN'; seq = 'CURRENT'; skey = ' '; keyl = 0; inset = ' '; input @; if stat ne '0000' then go to staterr; 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. @97 startdate yymmdd6. @103 termdate 6.0 @109 birthdate yymmdd6.; output; end; 8 else do; put 'WARNING: No EMPLOYEE record for SORT key= ' emp '.'; put 'WARNING: Execution continues with next OFFICE CALC.'; put; _error_ = 0; end; end; else do; put 'WARNING: No OFFICE record for CALC key= 'offkey '.'; put 'WARNING: Execution continues with next OFFICE CALC.'; put; _error_ = 0; end; return; 9 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= skey=; put @1 'ERROR: DATA step execution terminating.'; _error_ = 0; stop; run; proc print data=work.emp; format startdate birthdate date9.; title1 'Office and Employee Information'; title2 'as Specified in Transaction Data Set'; run;
| |
| |
| |
| |
| |
| |
| |
| |
|
The following output shows a portion of the output from this program.
Using a Data Set to Locate Records
Office and Employee Information as Specified in Transaction Data Set office_ office_ office_ Obs code office_street office_city state zip officephone1 1 001 20 W BLOOMFIELD ST SPRINGFIELD MA 02076 369772100 2 002 567 BOYLSTON ST BOSTON MA 02243 956237795 3 008 910 E NORTHSOUTH AVE WESTON MA 02371 367919136 employee_ Obs officephone2 officephone3 areacode speeddial id firstname 1 0 0 3 JENNIFER 2 625719562 398000000 69 JUNE 3 792923671 327000000 458 RICHARD Obs lastname street city state zip phone status 1 GARFIELD 110A FIRTH ST STONEHAM MA 02928 6173321967 01 2 BLOOMER 14 ZITHER TERR LEXINGTON MA 01675 6175555544 01 3 WAGNER 677 GERMANY LN NATICK MA 02178 6174321109 01 Obs ssnumber startdate termdate birthdate 1 021994516 21JAN1977 0 18AUG1945 2 039557818 05MAY1980 0 25APR1960 3 011776663 07JUN1978 0 04MAR1934
![]() |
![]() |
Copyright © 2007 by SAS Institute Inc., Cary, NC, USA. All rights reserved.