<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Weekly Workbench Tip: Bonus function for python users in SAS Viya Workbench Discussion</title>
    <link>https://communities.sas.com/t5/SAS-Viya-Workbench-Discussion/Weekly-Workbench-Tip-Bonus-function-for-python-users/m-p/957982#M19</link>
    <description>&lt;P&gt;&lt;STRONG&gt;A bit of context:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;I have had my share of developing performing data massaging, data wrangling, analytics &amp;amp; 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 &lt;STRONG&gt;interactive kernels&lt;/STRONG&gt; are quite popular for&amp;nbsp;data analysis, visualization, and modeling. The interactive feel&amp;nbsp;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. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;SAS VS code extension comes with the feature called SAS Notebooks that provide an exceptional capability of working on various languages all at once - &lt;STRONG&gt;SAS, Python, and SQL. &lt;/STRONG&gt;The capability provides flexibility to user to interact with Data in the language of choice and need.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ajaypanjwani_1-1738565012560.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104243i85549B6373D5BE97/image-size/large?v=v2&amp;amp;px=999" role="button" title="ajaypanjwani_1-1738565012560.png" alt="ajaypanjwani_1-1738565012560.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What I noticed:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;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&amp;nbsp;to look my dataframe output as a printed table which is easy to read and understand.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The expectation:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;A function that simply understands the python objects and use the appropriate method to output the results inline.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Something like:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ajaypanjwani_2-1738565624294.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104244iC21CE23F190D35BB/image-size/large?v=v2&amp;amp;px=999" role="button" title="ajaypanjwani_2-1738565624294.png" alt="ajaypanjwani_2-1738565624294.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;This should be even true for other objects such as lists, dictionaries, numerics, strings, &amp;amp; plots&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The Solution:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;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&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Viya-Workbench-Discussion/bd-p/viya-workbench-discussion" target="_blank" rel="noopener"&gt;SAS Viya Workbench Discussion&lt;/A&gt;&amp;nbsp;&lt;LI-MESSAGE title="Modifying the SAS Autoexec File in SAS Viya Workbench" uid="947907" url="https://communities.sas.com/t5/SAS-Viya-Workbench-Getting/Modifying-the-SAS-Autoexec-File-in-SAS-Viya-Workbench/m-p/947907#U947907" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-tkb-thread lia-fa-icon lia-fa-tkb lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;, &lt;LI-MESSAGE title="Weekly Workbench Tip: Run a SAS Notebook, %INCLUDE style" uid="943690" url="https://communities.sas.com/t5/SAS-Viya-Workbench-Discussion/Weekly-Workbench-Tip-Run-a-SAS-Notebook-INCLUDE-style/m-p/943690#U943690" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;). A complete definition of function show is provided below:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;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;")&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The Results:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Calling the show function provides an alternate to print function in python to display the python objects inline in an interactive session:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ajaypanjwani_3-1738567100435.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104245i82B05F99E522F49D/image-size/large?v=v2&amp;amp;px=999" role="button" title="ajaypanjwani_3-1738567100435.png" alt="ajaypanjwani_3-1738567100435.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ajaypanjwani_4-1738567138362.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104246i7DC63CE79B102BEE/image-size/large?v=v2&amp;amp;px=999" role="button" title="ajaypanjwani_4-1738567138362.png" alt="ajaypanjwani_4-1738567138362.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Closing Notes:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;It would be great to see SAS Community can provide more and more enhancement to &lt;U&gt;&lt;STRONG&gt;show&amp;nbsp;&lt;/STRONG&gt;&lt;/U&gt;function written above in solution section.&amp;nbsp;Feel free to comment and provide your suggestions. I hope this helps.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 03 Feb 2025 07:27:13 GMT</pubDate>
    <dc:creator>ajaypanjwani</dc:creator>
    <dc:date>2025-02-03T07:27:13Z</dc:date>
    <item>
      <title>Weekly Workbench Tip: Bonus function for python users</title>
      <link>https://communities.sas.com/t5/SAS-Viya-Workbench-Discussion/Weekly-Workbench-Tip-Bonus-function-for-python-users/m-p/957982#M19</link>
      <description>&lt;P&gt;&lt;STRONG&gt;A bit of context:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;I have had my share of developing performing data massaging, data wrangling, analytics &amp;amp; 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 &lt;STRONG&gt;interactive kernels&lt;/STRONG&gt; are quite popular for&amp;nbsp;data analysis, visualization, and modeling. The interactive feel&amp;nbsp;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. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;SAS VS code extension comes with the feature called SAS Notebooks that provide an exceptional capability of working on various languages all at once - &lt;STRONG&gt;SAS, Python, and SQL. &lt;/STRONG&gt;The capability provides flexibility to user to interact with Data in the language of choice and need.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ajaypanjwani_1-1738565012560.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104243i85549B6373D5BE97/image-size/large?v=v2&amp;amp;px=999" role="button" title="ajaypanjwani_1-1738565012560.png" alt="ajaypanjwani_1-1738565012560.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;What I noticed:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;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&amp;nbsp;to look my dataframe output as a printed table which is easy to read and understand.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The expectation:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;A function that simply understands the python objects and use the appropriate method to output the results inline.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Something like:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ajaypanjwani_2-1738565624294.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104244iC21CE23F190D35BB/image-size/large?v=v2&amp;amp;px=999" role="button" title="ajaypanjwani_2-1738565624294.png" alt="ajaypanjwani_2-1738565624294.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;This should be even true for other objects such as lists, dictionaries, numerics, strings, &amp;amp; plots&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The Solution:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;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&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Viya-Workbench-Discussion/bd-p/viya-workbench-discussion" target="_blank" rel="noopener"&gt;SAS Viya Workbench Discussion&lt;/A&gt;&amp;nbsp;&lt;LI-MESSAGE title="Modifying the SAS Autoexec File in SAS Viya Workbench" uid="947907" url="https://communities.sas.com/t5/SAS-Viya-Workbench-Getting/Modifying-the-SAS-Autoexec-File-in-SAS-Viya-Workbench/m-p/947907#U947907" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-tkb-thread lia-fa-icon lia-fa-tkb lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;, &lt;LI-MESSAGE title="Weekly Workbench Tip: Run a SAS Notebook, %INCLUDE style" uid="943690" url="https://communities.sas.com/t5/SAS-Viya-Workbench-Discussion/Weekly-Workbench-Tip-Run-a-SAS-Notebook-INCLUDE-style/m-p/943690#U943690" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;&amp;nbsp;). A complete definition of function show is provided below:&lt;/FONT&gt;&lt;/P&gt;
&lt;PRE&gt;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;")&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;The Results:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Calling the show function provides an alternate to print function in python to display the python objects inline in an interactive session:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ajaypanjwani_3-1738567100435.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104245i82B05F99E522F49D/image-size/large?v=v2&amp;amp;px=999" role="button" title="ajaypanjwani_3-1738567100435.png" alt="ajaypanjwani_3-1738567100435.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="ajaypanjwani_4-1738567138362.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104246i7DC63CE79B102BEE/image-size/large?v=v2&amp;amp;px=999" role="button" title="ajaypanjwani_4-1738567138362.png" alt="ajaypanjwani_4-1738567138362.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Closing Notes:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;It would be great to see SAS Community can provide more and more enhancement to &lt;U&gt;&lt;STRONG&gt;show&amp;nbsp;&lt;/STRONG&gt;&lt;/U&gt;function written above in solution section.&amp;nbsp;Feel free to comment and provide your suggestions. I hope this helps.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2025 07:27:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Viya-Workbench-Discussion/Weekly-Workbench-Tip-Bonus-function-for-python-users/m-p/957982#M19</guid>
      <dc:creator>ajaypanjwani</dc:creator>
      <dc:date>2025-02-03T07:27:13Z</dc:date>
    </item>
  </channel>
</rss>

