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

Posted on 2012-09-06
Last Modified: 2012-09-14
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/";

proc sort data="data1" out=dataset2; 
 by var5; 

Open in new window

Second Example
%include "/users/myfolder/";

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

Open in new window

Question by:labradorchik
    LVL 8

    Accepted Solution

    Hi there labradorchik,

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

    %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;

    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

    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


    Author Comment

    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:
    export data1

    Open in new window

    In the SAS code:
    %include "/users/myfolder/";  
    let data1=%sysget(data1);
    proc sort data="&data1" out=dataset2; 
      by var5; 

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Creating and Managing Databases with phpMyAdmin in cPanel.
    This is about my first experience with programming Arduino.
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now