AKS
AKS

Reputation: 204

SAS DS2 put statement output to a file

I have a SAS DS2 program which prints the output in the screen from a data step as below, I would like to channel the output to a file. AFAIK, 'file' is a missing feature in DS2 since DS2 currently reads and writes to tables, can someone please let me know how to stream the output from ds2 program to a file ? Thanks.

 put '********* Body (html contents from a stream) **********/';
 put _body;

Regards, AKS

Upvotes: 0

Views: 615

Answers (1)

Joe
Joe

Reputation: 63424

DS2 does not have file i/o capabilities beyond SAS or SQL or similar targets. DS2 mostly exists for the purpose of enabling easy connectivity to Hadoop and Teradata and some other Big Data targets; hopefully it will be expanded in the future but it's still quite young right now and not very well utilized.

That said, there are some ... creative workarounds. One possible answer involves using the log. This isn't a great solution, but it does work.

Basically, turn off notes and source, then PROC PRINTTO redirects the log to the file you want, PUT to the log, and then redirect back and turn things back on. Warnings and errors still go to the log, so don't have any of those.

This is definitely not a great solution for production code. For production code, I would highly recommend writing to a SQL table or SAS dataset and then generating your output with an old fashioned data step. DS2 isn't currently intended for this sort of thing. It's possible packages will be written to do this more helpfully in the future even if the language is not expanded to have this functionality; the JSON package is a good place to look to begin with, though I think it does not have the functionality right now.

Here's an example of using the log (a highly contrived one):

proc sql;
  select name into :namestr
    separated by ' '
    from sashelp.class
  ;
  select age into :agestr
    separated by ' '
    from sashelp.class;
quit;

%let namestr = %str(%')&namestr%str(%');
%let agestr  = %str(%')&agestr%str(%');

options nonotes nosource;
proc printto log="c:\temp\testds2.txt" new;
proc ds2;
  data _null_;
    method init();        
        dcl int rc;
        dcl nvarchar(15) name;
        dcl int age;
        dcl double iter;
        dcl nvarchar(1000) namestr;
        dcl nvarchar(500) agestr;
        name ='';
        age  = .;
        namestr= &namestr.;
        agestr = &agestr.;
        do iter = 1 to countw(namestr);
          name = scan(namestr,iter);
          age  = scan(agestr,iter);
          put name  age;
        end;
    end;

  enddata;
  run;
quit;
proc printto; run;
options notes source;

Upvotes: 2

Related Questions