Jun-24-2021, 11:53 PM
Hi guys,
I am a Pythong newbie and I am coding using the sasPy package. I am just running an initial script from SAS to test integration and I am getting the following type error once I run the Python function:
I am sure this is a simple one but it is a needle in the haystack scenario for me right now.
Really appreciate any help.
Cheers,
Simon
I am a Pythong newbie and I am coding using the sasPy package. I am just running an initial script from SAS to test integration and I am getting the following type error once I run the Python function:
Error:ERROR: masExecute encountered a failure in sfExecute, rc=0x8B3FF003.
ERROR: Micro Analytic Service encountered a failure.
ERROR: Python execution: Processing Return Argument: Invalid type(<class 'numpy.float64'>), value(3.1787822981106726)
ERROR: Call to foreign language function 'weightCalc' failed.
ERROR: Error reported in function 'python:CALL' in statement number 6 at line 35 column 2.
The statement was:
0 (35:2) rc = python:CALL( "weightCalc", hgt=2 )
The offending code is as below. proc fcmp outlib=work.funcs.pyFuncs; /* Define SAS Function */ function getItDone(hgt); /* Declare Object that is type Python */ declare object py(python); /* Insert Python Source Code */ submit into py; [b]# Import Python Modules into Session import pandas as pd import numpy as np import saspy # Connect to SAS Workspace Server using sasPy sas = saspy.SASsession(cfgfile='D:\Program Files\Python38\Lib\site-packages\saspy\sascfg_iomcom.py', user='xxxxx', pw='xxxxxxx') # Split a SAS Help Table into 2 Pandas Data Frames (for fun) class2Df1 = sas.sd2df (table="class", libref="sasHelp", dsopts={"keep": ["name","height"]}) class2Df2 = sas.sd2df (table="class", libref="sasHelp", dsopts={"keep": ["name","weight"]}) # Reconstruct the 2 DFs into 1 DF classJoinDf = pd.merge(class2Df1, class2Df2, how="inner", on='Name') # Calculate Average Weight:Height Ratio in Class weight2height = (classJoinDf["Weight"] / classJoinDf["Height"]).mean() # Function to Apply the Average Ratio to Each Height to Predict Weight def weightCalc(height): "Output: storeIt" # Predict Weight from Height predWeight = (height * weight2height) return predWeight, endsubmit; /* Send Python Source Code to Python Interpreter for Compilation */ rc = py.publish(); /* Call Python Code and Store Results in Python Dictionary */ rc = py.call("weightCalc", hgt); /* Return Results from Python Dictionary and Write to SAS Log */ result = py.results["storeIt"]; return(result);[/b] endsub; run; /* Include Compiler Subroutines during SAS Program Compilation */ options cmplib=work.funcs; /* Create Test Dataset */ data work.testTable; input var1; cards; 2 4 8 16 run; /* Test New SAS Function by Applying it on Test Dataset */ data work.testResult; set work.testTable; x = var1 * getItDone(var1); run;I am creating a result by dividing the average of 2 columns in a data frame and then trying to pass the result out of the python function back to SAS but there appears to be an issue with types when I carry the value of "predWeight" out of the function.
I am sure this is a simple one but it is a needle in the haystack scenario for me right now.
Really appreciate any help.
Cheers,
Simon