Example of Using DATA Step Views

The preceding introductory DATA step example can also be made into a DATA step view. A DATA step view is a SAS data set of type VIEW. It contains only a definition of data that is stored elsewhere, in this case, in a DL/I database; the view does not contain the physical data.
A DATA step view is a stored, named DATA step program that you can specify in other SAS procedures to access IMS data directly. A view's input data can come from one or more sources, including external files and other SAS data sets.
The following DATA step code is contained in a macro that is invoked twice to create two distinct DATA step views. When the DATA step views are executed, CUSTOMER segments are read from the AcctDBD database and selected data values are placed in two SAS data sets. Then each SAS data set is processed with PROC SORT and PROC PRINT to produce the same outputs as the introductory example in An Introductory Example of a DATA Step Program.
The numbered comments following this program correspond to the numbered statements in the program:
1 %macro custview(viewname=,p1=,p2=,p3=,p4=,p5=,
      p6=,p7=,p8=,p9=,p10=);
2 data &viewname / view=&viewname;
3 keep &p1 &p2 &p3 &p4 &p5 &p6 &p7 &p8 &p9 &p10;
4 infile acctsam dli status=st pcbno=2;
    input @1   soc_sec_number $char11.
          @12  customer_name  $char40.
          @52  addr_line_1    $char30.
          @82  addr_line_2    $char30.
          @112 city           $char28.
          @140 state          $char2.
          @142 country        $char20.
          @162 zip_code       $char10.
          @172 home_phone     $char12.
          @184 office_phone   $char12.;

    if st ¬= '  ' then
       do;
          file log;
          put _all_;
          abort;
       end;
5 %mend;
6 %custview(viewname=work.phone, 
        p1=customer_name, 
        p2=home_phone, 
        p3=office_phone);
7 %custview(viewname=work.address, 
        p1=customer_name, 
        p2=addr_line_1, 
        p3=addr_line_2,
        p4=city, 
        p5=state, 
        p6=country, 
        p7=zip_code);

 options linesize=132;
8 data work.phonlist;
    set work.phone;
 run;
9 proc sort data=work.phonlist;
    by customer_name;
 run;

 proc print data=work.phonlist;
    title2 'Customer Phone List';
 run;
10 data work.addrlist;
    set work.address;
 run;
11 proc sort data=work.addrlist;
    by customer_name;
 run;

proc print data=work.addrlist;
   title2 'Customer Address List';
run;
1 %MACRO defines the start of the macro CUSTVIEW which allows 11 input overrides. VIEWNAME is the name of the DATA step view to be created. The following are the other 10 overrides:
P1
name of the 1st data item name to keep.
P2
name of the 2nd data item name to keep.
P3
name of the 3rd data item name to keep.
P4
name of the 4th data item name to keep.
P5
name of the 5th data item name to keep.
P6
name of the 6th data item name to keep.
P7
name of the 7th data item name to keep.
P8
name of the 8th data item name to keep.
P9
name of the 9th data item name to keep.
P10
name of the 10th data item name to keep.
Ten data items are allowed because there are 10 input fields in the INPUT statement for the database.
2 The DATA statement names the DATA step view as specified by the macro variable &VIEWNAME.
3 The KEEP statement identifies the variables that comprise the observations in the output data set. In this case, there are as many as 10.
4 This is the same code that was executed in the introductory example in An Introductory Example of a DATA Step Program.
5 %MEND defines the end of macro CUSTVIEW.
6 %CUSTVIEW generates a DATA step view named Work.Phone, which when executed produces observations containing the data items CUSTOMER_NAME, HOME_PHONE, and OFFICE_PHONE.
7 %CUSTVIEW generates a DATA step view named Work.Address, which when executed produces observations containing the data items CUSTOMER_NAME, ADDR_LINE_1, ADDR_LINE_2, CITY, STATE, COUNTRY, and ZIP_CODE.
8 Data set Work.PhoneList is created by obtaining data using the DATA step view Work.Phone.
9 PROC SORT sorts Work.PhonList and PROC PRINT prints it out.
10 Data set Work.AddrList is created by obtaining data using the DATA step view Work.Address.
11 PROC SORT sorts Work.AddrList and PROC PRINT prints it out.