Writing to an in-memory array with PUT

From sasCommunity
Jump to: navigation, search

This code writes to an in-memory array using the PUT statement. Although there ought to be a builtin way to do this (there was in the data step language's model, PL/I), there's not. This provides a workaround.

The general idea is create a memory buffer using FILE and INFILE with SHAREBUFFERS; when the file is written to, the value of the automatic variable _INFILE_ is set. You can then copy _INFILE_ to a member of your array. Only one actual I/O operation takes place, because everything is buffered.

The same technique works for a single variable; this example uses an array because that's what I needed when I created the original code.

filename buffer catalog 'work.io.buffer.source' lrecl=100 recfm=f;
 
data _null_;
   file buffer lrecl=100 recfm=f;
   put ' ';
run;
 
options stimer fullstimer;
 
data _null_;
 
   array claims (1000) $100. _temporary_ (1000 * ' ');
 
   infile buffer sharebuffers lrecl=100 recfm=f;
   file buffer;
 
   input @@;  /* Make sure buffer exists */
 
   do i = 1 to 1000;
      put @1 'a b c ' i comma6.0 @@;
      claims(i) = _infile_;
   end;
 
   file log; 
 
   do i = 1 to 5, 996 to 1000;
      put i= claims{i}=;      
   end;
 
   stop;
 
run;

See also the documentation for the _INFILE_ and _FILE_ variables. On some cases, _FILE_ will be all you need.