4

This is a follow up question to a previous post. I am trying to store a macro in a catalog as outlined by these articles, in addition to the resources cited in my original post:

Here is what I have done so far:

I have created a directory "C:\myMacros." First, I create a program called "HelloWorld.sas" which contains the following code:

/* HelloWorld.sas */
option mstored sasmstore=mymacros;
libname myMacros 'C:\myMacros';

%macro HelloWorld() / store source;
    data _null_;
        put "Hello, World!";
    run;
%mend;

The code executes with the following log entry:

1    option mstored sasmstore=mymacros;
2    libname myMacros 'C:\myMacros';
NOTE: Libref MYMACROS was successfully assigned as follows:
      Engine:        V9
      Physical Name: C:\myMacros
3
4    %macro HelloWorld() / store source;
5        data _null_;
6            put "Hello, World!";
7        run;
8    %mend;

Within the SAS File Explorer, the Active Library "Mymacros" has been created in which resides catalog named "Sasmacr". Checking within Windows Explorer, I see that "C:\myMacros\sasmacr.sas7bcat" has been created. In the SAS File Explorer, I click on the "Sasmacr" catalog and find "Helloworld" inside. When I double click on it, I get message box saying,

No default action for the Macro data type.

Therefore, I conclude that the macro has been compiled and stored in the "Sasmacr" catalog. I close out of SAS to clear all memory.

Now, I try to call the macro. I open a new SAS session and create a new program titled "CallHelloWorld1.sas" which contains the following code:

/* CallHelloWorld1.sas */
libname myMacros 'C:\myMacros';
filename myCat catalog 'mymacros.sasmacr.helloworld.source';
%include myCat;

%HelloWorld();

This generates an error at the %include statement.

1    /* CallHelloWorld1.sas */
2    libname myMacros 'C:\myMacros';
NOTE: Libref MYMACROS was successfully assigned as follows:
      Engine:        V9
      Physical Name: C:\myMacros
3    filename myCat catalog 'mymacros.sasmacr.helloworld.source';
4    %include myCat;
ERROR: Physical file does not exist, SOURCE  .
ERROR: Cannot open %INCLUDE file MYCAT.
5
6    %HelloWorld();
     -
     180
WARNING: Apparent invocation of macro HELLOWORLD not resolved.

ERROR 180-322: Statement is not valid or it is used out of proper order.

The error claims that the physical file doesn't exist, which contradicts my observation of it above. So, I conclude that I am calling it incorrectly. According to FILENAME Statement, CATALOG Access Method, a SAS four part name consists of library.catalog.entry.entrytype. My statement consists of

  1. The library: myMacros as defined by libname myMacros 'C:\myMacros';
  2. The catalog: sasmacr
  3. The entry: helloworld
  4. The type: source

That is, mymacros.sasmacr.helloworld.source. There must be an error in here, but I cannot fathom what it is.

To try another approach, again I close out of SAS to clear the memory. I create "CallHelloWorld2.sas" which contains the following code:

/* CallHelloWorld2.sas */
libname myMacros 'C:\myMacros';
filename myCat catalog 'mymacros.sasmacr';
%include myCat(HelloWorld);

%HelloWorld();

This too produces an error at the %include line:

1    /* CallHelloWorld2.sas */
2    libname myMacros 'C:\myMacros';
NOTE: Libref MYMACROS was successfully assigned as follows:
      Engine:        V9
      Physical Name: C:\myMacros
3    filename myCat catalog 'mymacros.sasmacr';
4    %include myCat(HelloWorld);
ERROR: Entry HELLOWORLD.SOURCE not found in catalog MYMACROS.SASMACR.
ERROR: Cannot %INCLUDE member HelloWorld in the aggregate MYCAT.
ERROR: Entry HELLOWORLD.SOURCE not found in catalog MYMACROS.SASMACR.
ERROR: Cannot %INCLUDE member HelloWorld in the aggregate MYCAT.
5
6    %HelloWorld();
     -
     180
WARNING: Apparent invocation of macro HELLOWORLD not resolved.

ERROR 180-322: Statement is not valid or it is used out of proper order.

It appears that either my macro has not been stored properly or that I am calling it incorrectly. However, the resources are woefully inadequate. Please help!

Community
  • 1
  • 1
Lorem Ipsum
  • 4,020
  • 4
  • 41
  • 67

1 Answers1

4
libname myMacros 'C:\temp';

option mstored sasmstore=mymacros;

%helloWorld()

That's all you need to do - remind SAS where you are pointing things and then run the macro. You don't need to %include anything.

Joe
  • 62,789
  • 6
  • 49
  • 67
  • Thank you, but why doesn't it print the source to the log? I've tried adding `source` or `mautosource` in varying combinations with `sasautos=libref` and `sasmstore=libref` to the `option` statement, but the macro source code does not print to the log even though the macro was saved with the source option. – Lorem Ipsum Sep 15 '16 at 16:31
  • Read that article you posted again ("Ways to store..."). `%COPY` is your answer, ultimately. – Joe Sep 15 '16 at 16:35
  • When I write `%copy HelloWorld / source;` it outputs the macro source to the log, but `HelloWorld` is highlighted in red as though it is an error. Is my syntax incorrect and the log output a convenient side effect? Or, is the syntax correct and the SAS highlighting misleading? – Lorem Ipsum Sep 15 '16 at 17:27
  • 1
    The syntax highlighter just doesn't have a rule for the %COPY macro statement. There are many valid SAS commands that the syntax highlighting doesn't understand. – Tom Sep 15 '16 at 19:27