BookmarkSubscribeRSS Feed
ajaypanjwani
SAS Employee

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. 

 

ajaypanjwani_1-1738565012560.png

 

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:

ajaypanjwani_2-1738565624294.png

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:

 

ajaypanjwani_3-1738567100435.png

ajaypanjwani_4-1738567138362.png

 

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. 

 

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

Discussion stats
  • 0 replies
  • 1278 views
  • 3 likes
  • 1 in conversation