![]() Chapter Contents |
![]() Previous |
![]() Next |
| SAS Component Language: Reference |
interface reader; read: method return=num; cpy: method n: num return=string; endinterface;Use SAVECLASS to create an interface entry with two abstract methods, READ and CPY. The abstract methods are by definition the interface itself. Any class that supports this interface will need to supply the implementations for these methods.
class ddata extends data supported reader;and
class fdata extends data supported reader;Since DDATA and FDATA contain READ and CPY methods, no other changes are needed in the classes.
class iter;
private num varn nvars;
public reader r /(autocreate='n');
/* Constructor */
iter: method rdr: reader n: num;
varn = 1;
nvars = n;
r = rdr;
endmethod;
/* Check if there are more elements to iterate over */
more: method return=num;
dcl num more = ^r.read();
varn = 1;
return more;
endmethod;
/* Return the next element */
next: method return=string;
dcl string c = "";
c = r.cpy(varn);
varn + 1;
return c;
endmethod;
endclass;Several things require explanation for this class. First, note that
it has two private variables, varn and nvars, to keep track of where it is in the iteration process.
The other helper class uses the iterator to loop over the data in a reader.
class read;
private iter i;
read: method r: reader;
i = _new_ iter(r, 2);
endmethod;
loop:method;
do while(i.more());
dcl string s s2;
s = i.next();
s2 = i.next();
put s s2;
end;
endmethod;
_term: method /(state='O');
i._term();
_super();
endmethod;
endclass;The constructor will create a new iterator, and the LOOP method will
use it to loop over the data.
The SCL to use these classes is
init: dcl string filename; dcl fdata f; dcl ddata d; dcl read r; /* Read an external file */ filename = "some_file"; f = _new_ fdata(filename, "i"); r = _new_ read(f); r.loop(); r._term(); /* Read a dataset */ filename = "sasuser.x"; d = _new_ ddata(filename, "i", 2); r = _new_ read(d); r.loop(); r._term(); return;This code will successively read an external file and a data set.
![]() Chapter Contents |
![]() Previous |
![]() Next |
![]() Top of Page |
Copyright © 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.