#!/usr/bin/env python # coding: utf-8 # # Data Interchange Examples # # # In[1]: import feather import pandas as pd import saspy pd.show_versions() # In[2]: sas = saspy.SASsession() # # Example from: # https://github.com/sassoftware/saspy/blob/master/saspy_example_github.ipynb # # In[3]: cars = sas.sasdata('cars', libref='sashelp') # In[4]: print(type(cars)) # # SAS dataset SASHELP.CARS converted to panda dataframe car_df # In[5]: car_df = cars.to_df() # In[6]: print(type(car_df)) # # panda dataframe car_df converted to SAS dataset WORK.CARS # In[7]: car_df2 = sas.df2sd(car_df, 'cars') # In[8]: print(type(car_df2)) # # Read a dataframe from disk with the feather library created previously from a R dataframe # In[9]: path = '/home/sas/notebook/r_staff.feather' pd_staff = feather.read_dataframe(path) # In[10]: pd_staff.dtypes # In[11]: print(type(pd_staff)) # In[12]: print(pd_staff) # # # The dataframe pd_staff appears acceptable to Python, but is not acceptable to the sas.df2sd call # In[13]: sd_staff = sas.df2sd(pd_staff, 'staff') # # Copy the pd_staff dataframe with default deep=True # In[14]: copy_deep_true = pd_staff.copy() id(pd_staff) # In[15]: print(type(copy_deep_true)) id(copy_deep_true) # # Call the sas.df2sd method. The copied dataframe is not acceptable to this call. # In[16]: cdt = sas.df2sd(copy_deep_true, 'staff_t') # # Copy pd_staff operation with deep=false parameter # In[17]: copy_deep_false = pd_staff.copy(deep=False) id(pd_staff) # In[18]: print(type(copy_deep_false)) id(copy_deep_false) # # Call the sas.df2sd method. copy_deep_false dataframe is not acceptable. # In[19]: cdf = sas.df2sd(copy_deep_false, 'staff_f') # # Create the medals dataframe with the read.csv method # In[20]: df_medals = pd.read_csv("http://winterolympicsmedals.com/medals.csv") # In[21]: df_medals.head() # # Call the sas.df2sd method to create the SAs dataset WORK.medals2 # In[22]: sd_medals = sas.df2sd(df_medals, 'medals2') # In[23]: sd_medals.contents() # In[24]: sas # In[ ]: