Dan
Dan

Reputation: 10171

Define macro upon initially opening SAS

I ran across a really nice macro I'd like to use as a hotkey:

%macro closevts / cmd; 
  %local i; 
  %do i=1 %to 20;
    next "viewtable:"; end; 
  %end; 
%mend;

dm "keydef F12 '%NRSTR(%closevts);"; /*Sets F12 to close all view tables at once*/

(Source: https://stackoverflow.com/a/3254004/110797)

The problem is that I have to rerun the macro declaration everytime I start SAS! The above code doesn't permanently set the keybinding, but I figured that part out (just go in Keys (F9) and manually set F12 to %closevts, then save it as permanent when you close the keys window). How can I permanently set the macro definition too?

I looked using sas profiles, but I haven't been able to figure it out. Plus my system is rather locked down, so multiple solutions are appreciated in case some don't work.

Upvotes: 2

Views: 729

Answers (3)

Victor
Victor

Reputation: 17107

You can put your commands in the autoexec.sas file. This file will be invoked every time you open a SAS session.

Upvotes: 0

Joe
Joe

Reputation: 63434

Bob's solution is certainly a good one if you can do that.

Autocall macros are another option; you set your SASAUTOS directory to include a local directory:

Options Mautosource
  Sasautos=(’g:\busmeas\’,’k:\finance\’,’c:\product\’);

Then you just put the macro inside a file with only that macro in it, and named with the macro name (so %mymacro would be stored in mymacro.sas). SAS will look in that directory automatically when it gets a macro call and compile it when needed.

You also can run the macro in your autoexec, which you should be able to submit automatically even if you are totally locked down (put it as part of your shortcut). More detailed information is found here: http://www.sascommunity.org/wiki/Batch_processing_under_Windows
That's primarily about batch processing, but a lot of the notes are also useful for DM work.

For example, from that page, your shortcut might point to: sas -autoexec MyPersonalAutoExec.sas and any (normal SAS) code in that file would run on startup.

Upvotes: 2

BellevueBob
BellevueBob

Reputation: 9618

SAS macros are compiled and stored into your WORK library by default, which is why they "disappear" at the end of your session. You can use the macro statement store option to compile and create a permanent copy of your macro. The macro will be stored into a SAS catalog named sasmacr in whatever library is defined by the SASMSTORE system option. To use the stored macros in a future program, you will also need to use the MSTORED system option.

In your case, since you want to make this a "default" for your SAS session, you can use your SASUSER library, but it can be any library that is allocated. Just be sure you have write-access to the library when creating compiled macros.

So, in your case, use this program to compile and store your macro:

options mstored sasmstore=sasuser;

%macro closevts / cmd store; 
  %local i; 
  %do i=1 %to 20;
    next "viewtable:"; end; 
  %end; 
%mend;

After running the above, look inside your SASUSER library and you will see the catalog and the macro entry. Then add this statement to your autoexec.sas program so it executes each time you start a SAS session:

options mstored sasmstore=sasuser;

Upvotes: 5

Related Questions