Support for SAS language in org-babel
- Download (or clone)
ob-sas.el
and put it in your emacs directory (for example the directory/home/foouser/.emacs.d/lisp
) - To use SAS as another babel language, add the following to your
.emacs
(load "/home/foouser/.emacs.d/lisp/ob-sas.el") (require 'ob-sas) (require 'ob-R) (org-babel-do-load-languages 'org-babel-load-languages '((sas . t) (R . t) ))
This example show how to load R and SAS languages; if R is not needed it can be removed safely.
- Give the full path of sas executable in variable
org-babel-sas-command
. It can be done usingsetq
in your.emacs
orset-variable
in an interactive mode. The default is/usr/local/bin/sas_u8
(SAS unicode for unix). To set it you can add the following line in your.emacs
(setq org-babel-sas-command "C:\\Progra~1\\SASHome\\SASFoundation\\9.4\\sas.exe")
- Syntaxic coloration (optional): install
sas.el
. - sas and session (optional). If you are on
Unix/Linux, you can use a real session with sas.el:
i) install sas.el ii) set
org-babel-sas-realsession
to non nil. To set it you can add the following line in your.emacs
(setq org-babel-sas-realsession t)
Windows support is limited:
- “real session” is not possible due to SAS: only SAS table are present in session, no macro for instance.
- Moreover the submitted program have the following limitation “The maximum line length is 32767 bytes” (from SAS 9.4 Companion for Windows, Fifth Edition)
Org-Babel-sas try to propose the classical arguments of src blocks but there are several difficulties due to the structure of SAS language.
To use a true session, sasbis
is needed and works only with unixes
(SAS limitation). This can be done by setting
org-babel-sas-realsession
to non nil (nil is the default
value). For instance the following (setq
org-babel-sas-realsession t)
can be included in the .emacs
The true session ensure that all is saved in the session (useful for using macro-variables or macro-program from one src block to another)
The non true session (the default) ensures only that the SAS tables can be used from one src block to another (by using a user library).
:output
work as expected:value
SAS is not a functionnal language and:value
does not have sense. In order to use:value
another argument:sastab
is needed to point to the SAS table to be returned as a value. This is done by exporting with proc export the given SAS table in the:sastab
argument (thus any argument compatible with table argument of proc export will work)
SAS have two types of graphics: classical and ODS. Thus to use
graphics there is two arguments for :results
used in conjunction with file
: :results graphics file
and :results odsgraphics file
#+BEGIN_SRC sas :results output :session none proc print data=sashelp.birthwgt(firstobs=1 obs=3); run; #+END_SRC
using (setq org-babel-sas-realsession nil)
(the default value) and a
given user library directory under windows, a sas table can be created
(in this given directory/library) in one chunk and used in another (using the same session). One
drawback is that you need to deleted all created files if not needed
(see also next example)
#+BEGIN_SRC sas :results none :session "c:/users/pac/tmp" data blob; set sashelp.birthwgt(firstobs=1 obs=3); run; #+END_SRC #+BEGIN_SRC sas :results output :session "c:/users/pac/tmp" proc print data=blob; run; #+END_SRC
Org babel can take care of the directory name for the session (this directory session will be deleted when closing emacs)
#+BEGIN_SRC sas :session :results none data blob; set sashelp.birthwgt(firstobs=1 obs=3); run; #+END_SRC #+BEGIN_SRC sas :results output :session proc print data=blob; run; #+END_SRC
You can use a :session
to create a SAS table without any results
printed using :results none
. And on another chunk you can reuse the
SAS table, modify it and see the results in a “value” fashion.
To use :results value
you must add a :sastab
argument which is
the name of the SAS table you wish to see in the result value and you
can use options compatible with the data
argument of proc
export
. In this example I do not want the whole table but only the three
first lines of table blib
in the result value
#+BEGIN_SRC sas :results none :session data blob; set sashelp.birthwgt; run; #+END_SRC #+BEGIN_SRC sas :results value :session :sastab blib(firstobs=1 obs=3) data blib; set blob; drop Race; run; #+END_SRC
In order to get the graphics, you need to specify graphics
in
:results
and give filename in :file
. As in ob-R.el
the filename extension will specify the format
#+begin_src sas :results graphics file :file "Z:\\histogram.png" :session :exports both :comments org proc univariate data=sashelp.cars; histogram weight; run; #+end_src
As ODS graphics do not have identical export commands. To use ODS
graphics you must use odsgraphics
instead of graphics
. If you
want two or more graphics, the :file
will be understood as the
basename and SAS will export them as png files (the default).
#+begin_src sas :results odsgraphics file :file "plotreg" :session :exports both :comments org proc reg data=sashelp.cars plot(only)=(RESIDUALBYPREDICTED FITPLOT); model MPG_City=Weight; run; #+end_src
Log files are given in buffer. In order to debug programs log
can be substituted to results (easier than switching to log buffer) as follows:
#+BEGIN_SRC sas :results none :session data blob; set sashelp.birthwgt; run; #+END_SRC #+BEGIN_SRC sas :results log :session :sastab blib(firstobs=1 obs=3) data blib; set blob; drop Race; run; #+END_SRC