2

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.

Community
  • 1
  • 1
Dan
  • 9,935
  • 15
  • 56
  • 66
  • You don't need to use `%` to invoke a command macro on the command line. Just type the name of the macro like you would type any other command. – Tom Nov 13 '15 at 18:30

3 Answers3

5

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;
BellevueBob
  • 9,498
  • 5
  • 29
  • 56
  • I believe Joe's solution is nicer as it will allow multiple 'sessions' to use the macro. In Windows, only one SAS session may use a SAS profile at a time. If the macro has been stored in one profile it won't be available if you open up a second session. Interesting answer though - I didn't know about this option... – Robert Penridge May 07 '13 at 21:46
  • Although I guess in the OPs situation since he is setting up the keybinding in each profile it won't be so much an issue for him. – Robert Penridge May 07 '13 at 21:53
  • 1
    @RobPenridge Well, actually, only one SAS session can have WRITE access to the same SAS profile; if sessions are started with the `-rsasuser` option, you they can get read-only access. But I agree, it might be better to use a library other than `SASUSER`. – BellevueBob May 07 '13 at 22:44
  • Oh very cool - i didn't know about the -rsasuser option for read-only access either! I can definitely use this... Thanks. – Robert Penridge May 08 '13 at 16:09
2

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.

Joe
  • 62,789
  • 6
  • 49
  • 67
  • 2
    Note that in a *NIX environment the macro names and the filenames are case sensitive. So if you have a camelcase macro name such as `%DoSomeThing()` then the filename must match that case `DoSomeThing.sas`. It's an easy mistake to make if you haven't run into it before... – Robert Penridge May 07 '13 at 21:48
  • To build on the comment by @RobertPenridge about nix platforms, how you call your macro in your program does not matter - %DoSomething() and %DOSOMETHING() execute the same macro. What matters is how you name the macro file in the autocall directory. Macro files must be created in lower case. If mixed or upper, SAS will not recognize the file. – floydn Feb 07 '23 at 19:36
0

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

Victor
  • 16,609
  • 71
  • 229
  • 409