问题
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.
回答1:
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()"
回答2:
Building off @Quentin's answer, if your datastep is huge you can write your datastep and save it as a compiled program as such:
/* store your datastep (below stored in WORK, can be stored in any defined library */
data male female / pgm=work.saved_program;
set sashelp.class;
if SEX="M" then output male;
else output female;
run;
Then as @Quentin suggested, go to your KEYS<DMKEYS>
window and enter:
submit "data pgm = work.saved_program; describe; execute; run;"
This will submit your entire datastep saved in any library you choose.
来源:https://stackoverflow.com/questions/42841397/sas-execute-sas-code-using-hotkey