{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Notebook Python SAS\n", "\n", "## Import des données dans Python" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0speeddist
0142
12410
2374
34722
45816
\n", "
" ], "text/plain": [ " Unnamed: 0 speed dist\n", "0 1 4 2\n", "1 2 4 10\n", "2 3 7 4\n", "3 4 7 22\n", "4 5 8 16" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pandas as pd\n", "# data_url = \"https://forge.scilab.org/index.php/p/rdataset/source/file/master/csv/datasets/cars.csv\"\n", "data_url = \"cars.csv\"\n", "df_python = pd.read_csv(data_url)\n", "df_python.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Supression de la première colonne" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
speeddist
042
1410
274
3722
4816
\n", "
" ], "text/plain": [ " speed dist\n", "0 4 2\n", "1 4 10\n", "2 7 4\n", "3 7 22\n", "4 8 16" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_python.drop(df_python.columns[[0]], axis=1, inplace=True)\n", "df_python.head()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary avec Python" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
countmeanstdmin25%50%75%max
speed50.015.405.2876444.012.015.019.025.0
dist50.042.9825.7693772.026.036.056.0120.0
\n", "
" ], "text/plain": [ " count mean std min 25% 50% 75% max\n", "speed 50.0 15.40 5.287644 4.0 12.0 15.0 19.0 25.0\n", "dist 50.0 42.98 25.769377 2.0 26.0 36.0 56.0 120.0" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_python.describe().transpose()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary avec SAS" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SAS Connection established. Subprocess id is 4708\n", "\n" ] }, { "data": { "text/plain": [ "Libref = WORK\n", "Table = _df\n", "Dsopts = {}\n", "Results = Pandas" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import saspy\n", "session_sas = saspy.SASsession(cfgname='winlocal')\n", "df_sas = session_sas.df2sd(df_python)\n", "df_sas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "- La fonction `df2sd()` produit un objet Python, ici `df_sas`, qui contient une table SAS nommée `_df`.\n", "- `SASPy` fournit des objets Python de haut niveau pour les principales procédures SAS.\n", "- Toutes les fonctions SAS sont exécutables à l'aide de la commande magique `%%SAS`.\n", "- On applique les fonctions Python sur l'objet `df_sas` et les fonctions SAS sur l'objet `_df`." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
VariableNNMissMedianMeanStdDevMinP25P50P75Max
0speed5001515.405.287644412151925
1dist5003642.9825.7693772263656120
\n", "
" ], "text/plain": [ " Variable N NMiss Median Mean StdDev Min P25 P50 P75 Max\n", "0 speed 50 0 15 15.40 5.287644 4 12 15 19 25\n", "1 dist 50 0 36 42.98 25.769377 2 26 36 56 120" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df_sas.describe()" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "Sortie SAS\r\n", "\r\n", "\r\n", "\r\n", "

Sortie SAS

\r\n", "
\r\n", "

La MEANS Procédure

\r\n", "
\r\n", "

Statistiques descriptives

\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "\r\n", "
VariableNMoyenneEcart-typeMinimumMaximum
\r\n", "
\r\n", "
speed
\r\n", "
dist
\r\n", "
\r\n", "
\r\n", "
\r\n", "
50
\r\n", "
50
\r\n", "
\r\n", "
\r\n", "
\r\n", "
15.40
\r\n", "
42.98
\r\n", "
\r\n", "
\r\n", "
\r\n", "
5.29
\r\n", "
25.77
\r\n", "
\r\n", "
\r\n", "
\r\n", "
4.00
\r\n", "
2.00
\r\n", "
\r\n", "
\r\n", "
\r\n", "
25.00
\r\n", "
120.00
\r\n", "
\r\n", "
\r\n", "
\r\n", "
\r\n", "\r\n", "\r\n" ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%%SAS session_sas\n", "ods title;\n", "ods noproctitle;\n", "proc means data=_df maxdec=2;\n", "run;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Liens utiles\n", "\n", "**Code SAS dans notebook Jupyter :**\n", "\n", "- https://blogs.sas.com/content/sasdummy/2016/04/24/how-to-run-sas-programs-in-jupyter-notebook/\n", "- https://blogs.sas.com/content/sasdummy/2017/04/08/python-to-sas-saspy/\n", "- https://sassoftware.github.io/saspy/\n", "\n", "**Export vers pdf :**\n", "\n", "- https://github.com/sassoftware/sas_kernel/issues/13\n", "- https://stackoverflow.com/questions/43965823/convert-notebook-generated-html-snippet-to-latex-and-pdf\n", "- https://github.com/jupyter/nbconvert/issues/474\n" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.0" } }, "nbformat": 4, "nbformat_minor": 2 }