A bit of context:
I have had my share of developing performing data massaging, data wrangling, analytics & modelling on various open-source and enterprise IDE(s). Notebooks are the IDE(s) that stood out and are liked by many data analyst across the globe. Notebooks due to easy-of-use interface and interactive kernels are quite popular for data analysis, visualization, and modeling. The interactive feel appeals to a lot of developers and data scientists—everything’s in one place, and you get to experiment with code and see results instantly.
SAS VS code extension comes with the feature called SAS Notebooks that provide an exceptional capability of working on various languages all at once - SAS, Python, and SQL. The capability provides flexibility to user to interact with Data in the language of choice and need.
What I noticed:
You would have observed that all the three codes above display the same results. Python just appears different as it prints everything as a log output. However, as a python user myself I like to look my dataframe output as a printed table which is easy to read and understand.
The expectation:
A function that simply understands the python objects and use the appropriate method to output the results inline.
Something like:
This should be even true for other objects such as lists, dictionaries, numerics, strings, & plots
The Solution:
I wrote a function that a user can simply incorporate at start of his notebook, or save in autoexec or %include the notebook where the function is saved (Do explore these methods on SAS Viya Workbench Discussion Modifying the SAS Autoexec File in SAS Viya Workbench , Weekly Workbench Tip: Run a SAS Notebook, %INCLUDE style ). A complete definition of function show is provided below:
def show(obj,title='Output'):
SAS.submit("title2 '"+title+"';")
try:
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.figure
if isinstance(obj, pd.DataFrame):
new_df=obj.head()
SAS.df2sd(new_df,"WORK.TEMP")
SAS.submit("proc print data=TEMP;run;")
elif isinstance(obj, pd.Series) or isinstance(obj, pd.Index):
new_df=pd.DataFrame(obj,columns=['Input']).head()
SAS.df2sd(new_df,"WORK.TEMP")
SAS.submit("proc print data=TEMP;run;")
elif 'savefig' in dir(obj):
SAS.pyplot(obj)
elif isinstance(obj, int) or isinstance(obj, str) or isinstance(obj, dict):
SAS.submit("ODS TEXT='"+title+" : "+str(obj).replace("'","\"")+"';")
elif isinstance(obj, list):
SAS.submit("ODS TEXT='"+title+" : "+','.join(map(str, mylist))+"';")
else:
print(f"This is an object of type {type(obj)}.")
print(obj)
except Exception as e:
new_df=pd.DataFrame(['Python Process Encontered an Error',str(e)],columns=['Input'])
SAS.df2sd(new_df,"WORK.TEMP")
SAS.submit("proc odstext data=TEMP;p''||Input;run;")
The Results:
Calling the show function provides an alternate to print function in python to display the python objects inline in an interactive session:
Closing Notes:
It would be great to see SAS Community can provide more and more enhancement to show function written above in solution section. Feel free to comment and provide your suggestions. I hope this helps.
... View more