• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 615
  • Last Modified:

How to call a SAS dataset as a variable/logical with a macro in Unix bash shell?

I am trying to call a file (dataset1.sas7bdat) with a macro, but want to do it as a variable/logical (data1). I guess, I need to reference data1 somehow, but how?
Any somments or example will be appriciated!
First example is what I am looking for but is not working and the second example is working fine:

First Example
%include "/users/myfolder/macro3.sas";

proc sort data="data1" out=dataset2; 
 by var5; 
%macro3;
run;

Open in new window


Second Example
%include "/users/myfolder/macro3.sas";

proc sort data="dataset1.sas7bdat" out=dataset2; 
 by var5; 
%macro3;
run;

Open in new window

0
labradorchik
Asked:
labradorchik
1 Solution
 
ShannonEECommented:
Hi there labradorchik,

When you define the macro you have the option of positional or keyword parameters.

eg
%macro testMacro1(inputdata, outputdata, method=3, messages=1);

Open in new window

Here inputdata and outputdata are positional parameters and the other 2, method and messages are keywork parameters which also can have default values provided..
You may have 0 or more of each type, however positional parameters MUST come first.

Inside the body of the macro the macro parameters are used as

&inputdata     &outputdata      &method    and     &messages

Generally macros should use parameters.  Your macro3 call has no parameters shown, which means either there are no formal parameters, or you are using the default choices for the keyword parameters.  Either way not a good policy.

If you redefine the macro3 to something like
%macro macro3(inputdata=, outputdata=);  
       %** keyword parameters with no defaults **;

%*      some code .... *;

   proc ZZZZ data = &inputdata;
  %* procedure statements **;
   tables  customer * location / output= &outputdata;

   run;

quit;

%mend;

Open in new window

then call up the macro with something like
%macro3(inputdata=dataset2, outputdata=dataset3);

Open in new window

However, it appears that you are struggling with the whole macro concept.  You have a sort first, which may be just the
proc sort data="dataset1.sas7bdat" out=dataset2; 
 by var5; 

Open in new window

but as you didn't include a
run; quit;

Open in new window

to finish it off I can't be sure.  There is not much more that could validly be in the sort procedure!  SAS will assume the previous proc step is finished when it get to a new data or proc step, however it generally is best to explicitly finish off each step.

Then you call the macro suggesting it is going to use  dataset1.  This is a bit peculiar (though still quite valid) as you have just used dataset1 to produce dataset2.

I strongly recommend that you rewrite the macro with formal parameters, so that you can then supply the name of the data sets involved when you call the macro.

Otherwise you will always be in a confusing situation of having to know the exact details of the macro to be able to successfully call it.

=====

In the proc sort you are using a direct file reference (to a file in the current directory) which isn't the best idea.  

If you define a library (say) myDir as
libname mydir ".";

Open in new window

then you can refer to those datasets by dataset names and not have to give file names.
proc sort data="dataset1.sas7bdat" out=dataset2;

Open in new window

becomes
proc sort data=myDir.dataset1 out=dataset2; 

Open in new window

Note in myDir.dataset1 myDir is a library name (work is assumed if you don't mention a library) and dataset1 is the member name.  Where there is a file reference (in quotes) the dot  => . <=   is just for specifying the extension. SAS then has to interpret the file reference as
<an anonymous library>.dataset1
This could also be the cause of some of your confusion with the data1 and dataset1.sas7bdat


ian
0
 
labradorchikAuthor Commented:
Hi ShannonEE,
Thank you very much for your comments and explanations!

Also, sorry I did not get back to you earlier! I tried few different ways of testing my code. After some testings I found the following code working fine for me:

In the Unix bash shell script which is running below sas program:
data1=dataset1.sas7bdat
export data1

Open in new window


In the SAS code:
%include "/users/myfolder/macro3.sas";  

let data1=%sysget(data1);

proc sort data="&data1" out=dataset2; 
  by var5; 
%macro3;
run;

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now