{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Using SAS with SoS" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Installation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A [`sas_kernel`](https://github.com/sassoftware/sas_kernel) is needed to use SAS with Jupyter. Although a sas kernel is part of the SAS University Edition, it is difficult to use SAS with SoS in this way because the University Edition is executed inside a VM.\n", "\n", "Because sas_kernel requires SAS 9.4 or higher and only works on Linux, you will need a Linux server with SAS installed, and a Jupyter server with sas_kernel, which can be on the same or differnt server as SAS. Please refer to sas_kernel [documentation](https://github.com/sassoftware/sas_kernel) for details.\n", "\n", "Note that sas_kernel can be used with SAS 9.3 but some features might not work correctly." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Data Exchange" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A SAS dataset is roughly equivalent to Python's DataFrame (Pandas) and R's dataframe, and this is the only datatype that can be exchanged between SAS and other languages." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For example, if you have a Python DataFrame" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true, "kernel": "SoS" }, "outputs": [], "source": [ "import pandas as pd\n", "import numpy as np\n", "df = pd.DataFrame(np.random.randn(8, 3), columns=['A', 'B', 'C'])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use magic `%get` in SAS subkernel to get the dataframe as a SAS dataset" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "kernel": "SAS" }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "
The SAS System

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
ObsABC
1 0.43531-0.59014-0.53482
2 0.52063-0.07271 0.20335
3-0.33676-0.46688 0.67523
4 2.05154 0.49498-0.09263
5 0.14540 1.42063-1.53003
6-0.85585-1.43616 0.85540
7 1.69783-1.10050-0.20532
8 1.49746 0.56832-1.29725
\n", "
\n", "
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%get df\n", "proc print;\n", "run;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also `%get` variables from other subkernels using option `--from`" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "kernel": "SAS" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "Loading required package: feather\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "
The SAS System

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Obsmpgcyldisphpdratwtqsecvsamgearcarb
121.06160.01103.902.62016.460144
221.06160.01103.902.87517.020144
322.84108.0 933.852.32018.611141
421.46258.01103.083.21519.441031
518.78360.01753.153.44017.020032
618.16225.01052.763.46020.221031
714.38360.02453.213.57015.840034
824.44146.7 623.693.19020.001042
922.84140.8 953.923.15022.901042
1019.26167.61233.923.44018.301044
1117.86167.61233.923.44018.901044
1216.48275.81803.074.07017.400033
1317.38275.81803.073.73017.600033
1415.28275.81803.073.78018.000033
1510.48472.02052.935.25017.980034
1610.48460.02153.005.42417.820034
1714.78440.02303.235.34517.420034
1832.44 78.7 664.082.20019.471141
1930.44 75.7 524.931.61518.521142
2033.94 71.1 654.221.83519.901141
2121.54120.1 973.702.46520.011031
2215.58318.01502.763.52016.870032
2315.28304.01503.153.43517.300032
2413.38350.02453.733.84015.410034
2519.28400.01753.083.84517.050032
2627.34 79.0 664.081.93518.901141
2726.04120.3 914.432.14016.700152
2830.44 95.11133.771.51316.901152
2915.88351.02644.223.17014.500154
3019.76145.01753.622.77015.500156
3115.08301.03353.543.57014.600158
3221.44121.01094.112.78018.601142
\n", "
\n", "
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%get mtcars --from R\n", "proc print;\n", "run;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you have a dataset in SAS" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "kernel": "SAS", "scrolled": true }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "
The SAS System

\n", "
\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
ObsNAMESEXAGEHEIGHTWEIGHT
1JOHNM1259 99.5
2JAMESM1257 83.0
3ALFREDM1469112.5
4ALICEF1356 84.0
\n", "
\n", "
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "DATA CLASS;\n", " INPUT NAME $ 1-8 SEX $ 10 AGE 12-13 HEIGHT 15-16 WEIGHT 18-22;\n", "CARDS;\n", "JOHN M 12 59 99.5\n", "JAMES M 12 57 83.0\n", "ALFRED M 14 69 112.5\n", "ALICE F 13 56 84.0\n", "proc print;\n", "run;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can analyze it in SAS" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "kernel": "SAS" }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "SAS Output\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "
The SAS System

\n", "
The MEANS Procedure
\n", "

\n", "

\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
VariableNMeanStd DevMinimumMaximum
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
AGE
HEIGHT
WEIGHT
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
4
4
4
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
12.7500000
60.2500000
94.7500000
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
0.9574271
5.9651767
14.0386372
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
12.0000000
56.0000000
83.0000000
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
14.0000000
69.0000000
112.5000000
\n", "
\n", "
\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "PROC MEANS;\n", " VAR AGE HEIGHT WEIGHT;\n", "PROC PLOT;\n", " PLOT WEIGHT*HEIGHT;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or transfer the data to other subkernels for analysis" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "kernel": "R", "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
%preview CLASS
" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
> CLASS:
" ], "text/plain": [ ">>> CLASS:\n" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "\n", "\n", "\n", "\t\n", "\t\n", "\t\n", "\t\n", "\n", "
NAMESEXAGEHEIGHTWEIGHT
0JOHN M 12 59 99.5
1JAMES M 12 57 83.0
2ALFREDM 14 69 112.5
3ALICE F 13 56 84.0
\n" ], "text/latex": [ "\\begin{tabular}{r|lllll}\n", " & NAME & SEX & AGE & HEIGHT & WEIGHT\\\\\n", "\\hline\n", "\t0 & JOHN & M & 12 & 59 & 99.5 \\\\\n", "\t1 & JAMES & M & 12 & 57 & 83.0 \\\\\n", "\t2 & ALFRED & M & 14 & 69 & 112.5 \\\\\n", "\t3 & ALICE & F & 13 & 56 & 84.0 \\\\\n", "\\end{tabular}\n" ], "text/markdown": [ "\n", "| | NAME | SEX | AGE | HEIGHT | WEIGHT | \n", "|---|---|---|---|\n", "| 0 | JOHN | M | 12 | 59 | 99.5 | \n", "| 1 | JAMES | M | 12 | 57 | 83.0 | \n", "| 2 | ALFRED | M | 14 | 69 | 112.5 | \n", "| 3 | ALICE | F | 13 | 56 | 84.0 | \n", "\n", "\n" ], "text/plain": [ " NAME SEX AGE HEIGHT WEIGHT\n", "0 JOHN M 12 59 99.5 \n", "1 JAMES M 12 57 83.0 \n", "2 ALFRED M 14 69 112.5 \n", "3 ALICE F 13 56 84.0 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%get CLASS --from SAS\n", "%preview -n CLASS" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "kernel": "R" }, "outputs": [ { "data": { "text/html": [ "
\n", "\t
AGE
\n", "\t\t
12.75
\n", "\t
HEIGHT
\n", "\t\t
60.25
\n", "\t
WEIGHT
\n", "\t\t
94.75
\n", "
\n" ], "text/latex": [ "\\begin{description*}\n", "\\item[AGE] 12.75\n", "\\item[HEIGHT] 60.25\n", "\\item[WEIGHT] 94.75\n", "\\end{description*}\n" ], "text/markdown": [ "AGE\n", ": 12.75HEIGHT\n", ": 60.25WEIGHT\n", ": 94.75\n", "\n" ], "text/plain": [ " AGE HEIGHT WEIGHT \n", " 12.75 60.25 94.75 " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "apply(CLASS[3:ncol(CLASS)], 2, mean)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Session info" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "kernel": "SoS" }, "outputs": [ { "data": { "text/html": [ "

SoS

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
SoS Version
0.9.8.5
pandas
0.20.2
numpy
1.12.1
\n", "

SAS

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Kernel
sas
Language
SAS
\n",
       "For  Base  SAS  Software  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "   Image  version  information:  9.03.01M2P080112 \n",
       "For  SAS/STAT  ... \n",
       "   Custom  version  information:  12.1 \n",
       "   Image  version  information:  9.03.01M0P081512 \n",
       "For  SAS/GRAPH  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "For  SAS/ETS  ... \n",
       "   Custom  version  information:  12.1 \n",
       "   Image  version  information:  9.03.01M0P081512 \n",
       "For  SAS/FSP  ... \n",
       "   Custom  version  information:  9.3_M1 \n",
       "For  SAS/OR  ... \n",
       "   Custom  version  information:  12.2 \n",
       "   Image  version  information:  9.03.01M0P120512 \n",
       "For  SAS/AF  ... \n",
       "   Custom  version  information:  9.3 \n",
       "   Image  version  information:  9.03.01M0P051111 \n",
       "For  SAS/IML  ... \n",
       "   Custom  version  information:  12.1 \n",
       "   Image  version  information:  9.03.01M0P081512 \n",
       "For  SAS/QC  ... \n",
       "   Custom  version  information:  12.1 \n",
       "   Image  version  information:  9.03.01M0P081512 \n",
       "For  SAS/SHARE  ... \n",
       "   Custom  version  information:  9.3_M1 \n",
       "For  SAS/LAB  ... \n",
       "   Custom  version  information:  9.3 \n",
       "   Image  version  information:  9.03.01M0P081512 \n",
       "For  SAS/ASSIST  ... \n",
       "   Custom  version  information:  9.3 \n",
       "   Image  version  information:  9.03.01M0P051111 \n",
       "For  SAS/CONNECT  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "For  SAS/EIS  ... \n",
       "   Custom  version  information:  9.3 \n",
       "   Image  version  information:  9.03.01M0P051111 \n",
       "For  SAS/ACCESS  Interface  to  Netezza  ... \n",
       "   Custom  version  information:  9.3 \n",
       "   Image  version  information:  9.03.01M0P051111 \n",
       "For  SAS  Integration  Technologies  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "For  SAS/Secure  168-bit  ... \n",
       "   Custom  version  information:  9.3 \n",
       "   Image  version  information:  9.03.01M0P051111 \n",
       "For  SAS/ACCESS  Interface  to  DB2  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "For  SAS/ACCESS  Interface  to  Oracle  ... \n",
       "   Custom  version  information:  9.3_M1 \n",
       "For  SAS/ACCESS  Interface  to  Sybase  ... \n",
       "   Custom  version  information:  9.3_M1 \n",
       "For  SAS/ACCESS  Interface  to  PC  Files  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "For  SAS/ACCESS  Interface  to  ODBC  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "For  SAS/ACCESS  Interface  to  INFORMIX  ... \n",
       "   Custom  version  information:  9.3_M1 \n",
       "For  SAS/ACCESS  Interface  to  Teradata  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "For  SAS/ACCESS  Interface  to  Microsoft  SQL  Server  ... \n",
       "   Custom  version  information:  9.3_M2 \n",
       "For  SAS/ACCESS  Interface  to  MySQL  ... \n",
       "   Custom  version  information:  9.3_M2 
\n", "

R

\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
Kernel
ir
Language
R
R version 3.3.2 (2016-10-31)\n",
       "Platform: x86_64-pc-linux-gnu (64-bit)\n",
       "Running under: Red Hat Enterprise Linux Server release 6.3 (Santiago)\n",
       "\n",
       "locale:\n",
       " [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              \n",
       " [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    \n",
       " [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   \n",
       " [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 \n",
       " [9] LC_ADDRESS=C               LC_TELEPHONE=C            \n",
       "[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       \n",
       "\n",
       "attached base packages:\n",
       "[1] stats     graphics  grDevices utils     datasets  methods   base     \n",
       "\n",
       "other attached packages:\n",
       "[1] feather_0.3.1\n",
       "\n",
       "loaded via a namespace (and not attached):\n",
       " [1] Rcpp_0.12.8     assertthat_0.1  digest_0.6.10   crayon_1.3.2   \n",
       " [5] IRdisplay_0.4.4 repr_0.10       R6_2.2.0        jsonlite_1.1   \n",
       " [9] magrittr_1.5    evaluate_0.10   stringi_1.1.2   uuid_0.1-2     \n",
       "[13] IRkernel_0.7.1  tools_3.3.2     stringr_1.1.0   hms_0.3        \n",
       "[17] pbdZMQ_0.2-4    tibble_1.2     
\n" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%sessioninfo" ] } ], "metadata": { "kernelspec": { "display_name": "SoS", "language": "sos", "name": "sos" }, "language_info": { "codemirror_mode": "sos", "file_extension": ".sos", "mimetype": "text/x-sos", "name": "sos", "nbconvert_exporter": "sos_notebook.converter.SoS_Exporter", "pygments_lexer": "sos" }, "sos": { "default_kernel": "SoS", "kernels": [ [ "SAS", "sas", "SAS", "teal" ], [ "SoS", "sos", "", "" ], [ "R", "ir", "R", "#FDEDEC" ], [ "Bash", "bash", "Bash", "#E6EEFF" ], [ "javascript", "javascript", "", "" ], [ "julia-0.6", "julia-0.6", "", "" ], [ "python2", "python2", "", "" ], [ "python3", "python3", "", "" ], [ "sas", "sas", "", "" ] ], "panel": { "displayed": true, "height": 0, "style": "side" } } }, "nbformat": 4, "nbformat_minor": 2 }