Chapter Contents

Previous

Next
SAS Component Language: Reference

Other Classes and Further Abstraction -- Level Four

Given the reader interface, you can now use other classes - even ones outside the data class hierarchy - as readers, as long as they support the reader interface. Using the abstract reader interface enables you to read from many different types of objects as well.

For example, consider the following class, which uses SCL lists to maintain data:

class lst supports reader;
  private list l;
  private num nvars;
  private num nelmts;
  private num cur;

  /* Constructor */
  lst: method n:num;
       l = makelist();
       nvars = n;
       nelmts = 0;
       cur = 1;
  endmethod;

  /* Copy method */
  cpy: method n: num return=string;
        dcl string c = "";
        if (cur <= nelmts) then do;
          c = getitemc(l, cur);
          cur + 1;
        end;
        return c;
  endmethod;

 /* Read method */
  read: method return=num;
     if (cur > nelmts) then
         return 1;
     else
         return 0;
  endmethod;

  /* Add an element to the list */
  add: method c:string;
       nelmts + 1;
       setitemc(l, c, nelmts, 'Y');
  endmethod;

  /* Add two elements to the list */
  add: method c1:string c2:string;
        add(c1);
        add(c2);
  endmethod;

  /* Terminate the list */
  _term: method /(state='O');
      if (l) then dellist(l);
      _super();
  endmethod;
endclass;
This class represents a list, and because it supports the READER interface, it can be read in the same way as the DDATA and FDATA classes.

The SCL for reading from the list is

init:
 dcl lst l;
 dcl read r;
 l = _new_ lst(2);

 /* Notice the overloaded add method */
 l.add("123", "456");
 l.add("789", "012");
 l.add("345", "678");

 /* Create a read class and loop over the data */
 r = _new_ read(l);
 r.loop();

 r._term();
 return;
The output for this program will be
123 456
789 012
345 678


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.