Search code examples
macrossashotkeysshortcut

SAS: Execute SAS code using hotkey


I want to execute code which is independent of my current program via keyboard shortcuts within the Enhanced Editor in SAS 9.4 for Windows. I've achieved this with limited success being able to execute only macro statements. However, I want to be able to execute non-macro statements, too. How do I do this?

Here's what I've figured out so far.

General Setup

Get to the KEYS menu by either entering "KEYS" into the command prompt or submitting

dm 'keys';

For one of the keys, enter the definition

%put Hello, world!;

Save the new key binding by pressing Ctrl+s. For the purposes of this explanation, I will bind it to F7. Once saved, press F7 and "Hello, world!" will be printed to the log.

We can extend this concept further by placing the above code in a macro.

%macro HelloWorld();
  %put Hello, world!;
%mend;

Compile the %HelloWorld macro. If we then bind %HelloWorld(); to F7, we can demonstrate that a macro may be called with a shortcut.

Via AUTOCALL

We can take things further yet and save our %HelloWorld macro as a program HelloWorld.sas. If we then put this in an AUTOCALL library (run %put %sysfunc(pathname(sasautos)); to find where those are located on your computer), we can execute it within any new SAS session.

It appears, however, that only macro statements work with this method. To demonstrate this, suppose that we instead defined %HelloWorld as

%macro HelloWorld();
  data _null_;
    put 'Hello, world!';
  run;
%mend;

Again, save this as HelloWorld.sas and place it in an AUTOCALL directory. For me, when I try to execute this, I get the following error:

ERROR: The SAS/EIS product with which the procedure is associated is either not licensed for
       your system or the product license has expired. Please contact your SAS installation
       representative.

Via %INCLUDE

Since an AUTOCALL requires a macro to be compiled and called, I thought %INCLUDE might execute the code directly.

Create a file called HelloWorld.sas containing %put Hello, world!. Save it to a short file path. Then, in the KEYS menu bind F7 to %include "C:\Short Path\HelloWorld.sas";. Now F7 will print "Hello, world!" to the log.

If we instead save

data _null_;
  put 'Hello, world!';
run;

to HelloWorld.sas and try to run it using our %INCLUDE shortcut, I receive the same error:

ERROR: The SAS/EIS product with which the procedure is associated is either not licensed for
       your system or the product license has expired. Please contact your SAS installation
       representative.

Misc. Attempts

I've also tried entering code directly into a KEYS definition, but again, it only seems to work for macro statements.

It might be possible to use %SYSFUNC, but my ultimate goal is to be able to use PROC SQL or data steps and I don't think %SYSFUNC can do this.


Solution

  • You can use the submit command, i.e. define a key as:

    submit "data _null_ ; put 'Hello World!'; run;"
    

    Also works with a macro call:

    submit "%HelloWorld()"