Skip to content

Pierre-Andre/Org-Babel-sas

Repository files navigation

Org-Babel-sas

Support for SAS language in org-babel

Installation

  1. Download (or clone) ob-sas.el and put it in your emacs directory (for example the directory /home/foouser/.emacs.d/lisp)
  2. 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.

  3. Give the full path of sas executable in variable org-babel-sas-command. It can be done using setq in your .emacs or set-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")
        
  4. Syntaxic coloration (optional): install sas.el.
  5. 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)
        

Limitations

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)

Philosophy

Org-Babel-sas try to propose the classical arguments of src blocks but there are several difficulties due to the structure of SAS language.

session

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 results (except graphics)

  1. :output work as expected
  2. :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)

output graphics results

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

Examples

basic example: no session, simple output (no value, no graphics)

#+BEGIN_SRC sas :results output :session none 
proc print data=sashelp.birthwgt(firstobs=1 obs=3);
run;
#+END_SRC

simple output but with a given :session

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

simple output with :session

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

value output with :session

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

graphics

Classical graphics

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

ODS graphics

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

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

About

Support for SAS language in org-babel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published