Interaction between SAS® and Python for Data Handling and ...
Paper 3260-2019
Interaction between SAS? and Python for Data Handling and Visualization
Yohei Takanami, Takeda Pharmaceuticals
ABSTRACT
For drug development, SAS is the most powerful tool for analyzing data and producing tables, figures, and listings (TLF) that are incorporated into a statistical analysis report as a part of Clinical Study Report (CSR) in clinical trials. On the other hand, in recent years, programming tools such as Python and R have been growing up and are used in the data science industry, especially for academic research. For this reason, improvement in productivity and efficiency gain can be realized with the combination and interaction among these tools. In this paper, basic data handling and visualization techniques in clinical trials with SAS and Python, including pandas and SASPy modules that enable Python users to access SAS datasets and use other SAS functionalities, are introduced.
INTRODUCTION
SAS is fully validated software for data handling, visualization and analysis and it has been utilized for long periods of time as a de facto standard in drug development to report the statistical analysis results in clinical trials. Therefore, basically SAS is used for the formal analysis report to make an important decision. On the other hand, although Python is a free software, there are tremendous functionalities that can be utilized in broader areas. In addition, Python provides useful modules to enable users to access and handle SAS datasets and utilize SAS modules from Python via SASPy modules (Nakajima 2018). These functionalities are very useful for users to learn and utilize both the functionalities of SAS and Python to analyze the data more efficiently. Especially for Python users who are not familiar with or have to learn SAS code, SASPy modules are powerful tool that automatically generate and execute native SAS code via Jupyter Notebook bundled with Anaconda environment.
This paper is mainly focused on the basic functionalities, interaction and their differences between SAS and Python, therefore the advanced skills or functionalities for data handling and visualization are not covered. However, these introductions are useful for Python users who are not familiar with SAS code and vice versa. As shown in Figure 1, in this paper, it is assumed that the following versions of analysis environment are available on local PC (Windows 64 bit):
Windows PC SAS 9.4M3
SAS code is executed in SAS environment
BASE SAS and SAS/STAT are available
Anaconda 5.3.1 (Python 3.7)
Python code is executed in Jupyter Notebook
SASPy modules are executed in SAS session in Jupyter Notebook
1
SAS 9.4
Python (Jupyter Notebook with Anaconda)
Figure 1. SAS and Python (Jupyter Notebook in Anaconda) Environment
Table 1 shows the basic data handling and visualization modules of SAS and Python. Although only SAS dataset format is used in SAS, there are multiple data formats used in
Python such as Dataframe in Pandas module and Array in Numpy module.
Data Format Data Handling Data Visualization
SAS
SAS dataset (Array data can be used in the DATA Step as a part of dataset)
DATA Step (e.g. MERGE statement) and PROC step (e.g. SORT procedure, TRANSPOSE procedure)
PROC step for Graphics Procedure (e.g. SGPLOT, SGPANEL)
Python Dataframe (Pandas module), Array (Numpy module)
Pandas (e.g. DataFrame method, merge method, ), Numpy (e.g. arange method)
Matplotlib (e.g. plot, hist, scatter), Pandas (e.g. plot), Seaborn (e.g. regplot)
Table 1. Basic SAS and Python Modules for Data Handling and Visualization
In addition to the basic modules used for data handling and visualization in SAS and Python, Python SASPy modules to realize interactive process between them are introduced in a later chapter.
DATA HANDLING
In SAS, mainly data are manipulated and analyzed in SAS dataset format. On the other hand, in Python, there are some data formats used for data handling and visualization. The Dataframe format that corresponds to the SAS dataset in terms of data structure is mainly used in this paper.
READ SAS DATASET IN PYTHON
Although various kinds of data format (e.g. Excel, Text) can be imported and used in SAS and Python, it is more convenient for users to utilize the SAS dataset directly in Python in
2
terms of the interaction between them. Python can read SAS datasets with Pandas modules that enable users to handle these data in Dataframe format. For example, the following Python code simply reads a SAS dataset, test.sas7bdat, and converts it to the Dataframe format with the read_sas method in Pandas module:
import pandas as pd sasdt = pd.read_sas(r'C:\test\test.sas7bdat')
The test.sas7bdat is a simple dataset that includes only one row with three numeric variables, x, y and z.
Figure 2. SAS Dataset "Test"
Table 2 shows a Python code and output in Jupyter Notebook. After converting SAS dataset to Dataframe format, Pandas modules can handle it without any SAS modules. Columns in Dataframe correspond to variables in SAS dataset.
In: # import the pandas modules import pandas as pd # Convert a SAS dataset 'test' to a Dataframe 'sasdt' sasdt = pd.read_sas(r'C:\test\test.sas7bdat') print(sasdt)
Out:
x y z
0 1.0 1.0 1.0
Table 2. Conversion of SAS Dataset to Dataframe in Python
On the other hand, a Dataframe can be converted to a SAS dataset with the dataframe2sasdata() method in SASPy that is introduced in a later chapter:
# Export Dataframe to SAS dataset import saspy # Create SAS session sas = saspy.SASsession() # Create SAS library sas.saslib('test', path="C:/test") # Convert Dataframe to SAS dataset sas.dataframe2sasdata(df=sasdt, table='test2', libref='test')
SAS library "test" that is used for storing a SAS dataset "test2" is created using the sas.saslib method and a SAS dataset "test2.sas7bdat" is actually created in "C:/test" folder as shown in Figure 3.
3
Figure 3. SAS Dataset "Test2" Converted from a Dataframe
DATA MANUPILATION IN SAS AND PYTHON
As shown in Table 1, for data handling, mainly the DATA step is used in SAS and Pandas and Numpy modules are used in Python. In this section, some major modules and techniques for data manipulation are introduced in SAS and Python:
Creation of SAS dataset and Dataframe/Array Handling of rows and columns
Creation of SAS Dataset and Dataframe/Array Table 3 shows the data creation with simple SAS and Python codes:
SAS: Numeric and character variables are defined in the INPUT statement and data are listed in the CARDS statement. The PRINT procedure outputs the dataset "data1".
Python: Pandas modules are imported and the DataFrame method is used to create a Dataframe and the print method is used to output the Dataframe "data1".
SAS Dataset data data1 ;
input a b $ ; cards; 1 xxx 2 yyy ; run ; proc print data=data1 ; run ;
Output
Python Dataframe # Dataframe with numeric and character variables import pandas as pd data1 = pd.DataFrame([[1,'xxx'],[2,'yyy']],
columns=['a', 'b']) print(data1)
a b 0 1 xxx 1 2 yyy
Table 3. Creation of SAS dataset in SAS and Dataframe in Python
4
In Python, it should be noted that the row numbers are presented with data as shown in
Table 3 where the number begins with 0. This rule is applied to the element of data such as Pandas Dataframe and Numpy Array. For example, data1.loc[1,'a'] extracts 2, the value of
the 2nd row of column 'a' in the Dataframe data1.
As shown in Table 4, a SAS dataset and a Dataframe can be created more efficiently with other functionalities:
In SAS, the DO statement is used to generate consecutive values
In Python, firstly the array data are created with the arange method followed by the conversion to a Dataframe with the DataFrame and T methods. The T method transposes the Dataframe after combining col1 and col2 array data.
SAS Dataset creation data data2 ;
do a = 1 to 3 ; b = a*2 ; output ;
end ; run ; proc print data=data2 ; run ;
Output
Dataframe and Array in Python import pandas as pd import numpy as np # Create Array with Numpy module col1 = np.arange(1,4,1) # 1 to 3 by 1 col2 = col1*2 # Convert Array to Dataframe data2 = pd.DataFrame([col1,col2]).T data2.columns=['a','b'] print(data2)
a b 0 1 2 1 2 4 2 3 6
Table 4. Creation of SAS Dataset, Dataframe and Array
Handling of rows and columns Granted that a SAS dataset or Dataframe is successfully created, data transformation may be needed prior to the data visualization or analysis process. The following data handling techniques are introduced here: Addition and Extraction of Data Concatenation of SAS Datasets/Dataframe Handling of Missing Data
Addition and Extraction of Data The following example shows the addition of new variables/columns to SAS dataset/ Dataframe with simple manipulation.
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- introduction to big data with apache spark
- python nump and park
- improving python and spark performance and
- big data tutorial w2 spark
- pyarrow documentation
- spark cassandra integration theory practice
- apache spark guide cloudera
- cheat sheet for pyspark github
- building robust etl pipelines with apache spark
- pyspark standalone code
Related searches
- data classification and handling policy
- using sas for data analysis
- python read data from pdf
- python get data from pdf
- python aggregate data by column
- python change data type
- sas enterprise guide for beginners
- material handling and storage
- data collection and data analysis
- learn python for data science
- difference between tricare and tricare for life
- python difference between dictionary and list