Python Forum

Full Version: Incorrect Type Error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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:

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
(Jun-24-2021, 11:53 PM)milkycow Wrote: [ -> ]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:

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
Thanks Larz - Really appreciate your efforts here and I will do :)
Okay guys, I think I worked out what is happening..

The result is a numPy float64 array and it is falling over when trying to pass the result back to SAS.
Is there a means of converting the result to a standard float before it comes back to SAS? I have tried astype but I may have used it incorrectly.

Any help much appreciated.

Thanks,
Simon
(Jun-25-2021, 03:44 AM)milkycow Wrote: [ -> ]Okay guys, I think I worked out what is happening..

The result is a numPy float64 array and it is falling over when trying to pass the result back to SAS.
Is there a means of converting the result to a standard float before it comes back to SAS? I have tried astype but I may have used it incorrectly.

Any help much appreciated.

Thanks,
Simon


Okay all fixed. If anyone is interested, I needed to add .item() to the end of the mean calculation to ensure the type was "float" rather than "numPy float".

Have a good weekend everyone :)

Cheers,
Simon