Search code examples
sasincludesas-macro

How do you pass a SAS dataset as a paramteter in a macro?


I have a program "P" saved outside my project that I can call within any project use with an %include statement; it outputs a table called P_OUTPUT. I want to be able to pass a data table D that I've created within my project to "P" and have "P" use the information in the DataTable in an SQL statement in "P".

I'm not sure how to pass a data table as a parameter in SAS. I've created the table in WORK called WORK.D

%macro 
%let A = %sysfunc(open(work.D));
%include 'X:\P.sas';
%mend

in "P"

Proc SQL;
Create table P_OUTPUT AS 
SELECT …
WHERE Column IN (SELECT * FROM &D.)
;
Quit;

I get the error: ERROR: You cannot open WORK.D.DATA for output access with member-level control because WORK.D.DATA is in use by you in resource environment IOM ROOT COMP ENV.

How do I solution this?


Solution

  • To pass a dataset to a macro you can pass the NAME of the dataset. Example:

    %macro mymacro(indsname);
      proc print data=&indsname;
      run;
    %mend mymacro;
    %mymacro(indsname=sashelp.class);
    

    Your error message just means you already have that dataset open somewhere so SAS cannot open it again.

    Why do you have the call to the OPEN() function? That is not doing anything useful and might even be why you cannot create the dataset. If want WORK.D as the value of the macro variable D then just do that.

    %let D=work.D;
    

    Then when you expand D in your later code it generates valid SAS syntax.