sasincludedatastep

SAS include code into data step


I have dynamically create a myfile.sas with following content:

and a = 0
and b = 0

Now I want to include this file into a data step:

data y;
   set x;

   if 1=1 
      %include incl("myfile.sas")
   then selektion=0;
   else selektion=1;
run;

The result should be:

data y;
   set x;

   if 1=1 
      and a=0
      and b=0
   then myvar=0
   else myvar=1;
run;

However I get the following error:

ERROR 388-185: Expecting an arithmetic operator.
ERROR 200-322: The symbol is not recognized and will be ignored.

Is this possible to include the file into the if statement?


Solution

  • Indeed, that doesn't work. You can use %include within a data or proc step to add some lines to it but not within an incomplete statement.

    Had your myfile.sas looked like this:

    if 1=1
    and a = 0
    and b = 0
    

    you could have written

    data y;
       set x;
       %include "myfile.sas";;
       then selektion=0;
       else selektion=1;
    run;
    

    Couldn't you have these lines in a macro instead of a file?

    %macro mymacro;
      and a=0
      and b=0
    %mend;
    
    data y;
       set x;
       if 1=1 
          %mymacro
       then selektion=0;
       else selektion=1;
    run;
    

    If that myfile.sas has to stay as is, you could work around it in this rather convoluted (but still generic) way:

    filename myfile temp;
    
    data _null_;
    file myfile2;
    infile 'myfile.sas' eof=end;
    input;
    if _n_=1 then put '%macro mymacro;';
    put _infile_;
    return;
    end:
      put '%mend;';
    run;
    %include myfile;
    
    data y;
       set x;
       if 1=1 
          %mymacro
       then selektion=0;
       else selektion=1;
    run;