Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Incorrect Type Error
#1
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
Larz60+ write Jun-25-2021, 12:08 AM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use bbcode tags on future posts.
Reply
#2
(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
Reply
#3
Thanks Larz - Really appreciate your efforts here and I will do :)
Reply
#4
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
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Wrong type error rowan_bradley 6 1,146 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  Type Error: Unsupported Operand jhancock 2 1,068 Jul-22-2023, 11:33 PM
Last Post: jhancock
  Code is returning the incorrect values. syntax error 007sonic 6 1,137 Jun-19-2023, 03:35 AM
Last Post: 007sonic
  error 1102 (42000) incorrect database name 's' Anldra12 4 1,666 Jun-08-2022, 09:00 AM
Last Post: Anldra12
  Python Anytree - Is not of type 'NodeMixin' error georgebijum 3 2,027 May-05-2022, 01:43 PM
Last Post: Gribouillis
  openpyxl incorrect delete rows VladislavM 6 4,032 Jul-19-2021, 08:54 AM
Last Post: VladislavM
Star Type Error: 'in' object is not callable nman52 3 3,331 May-01-2021, 11:03 PM
Last Post: nman52
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 3,024 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  Type Error in Python MarcusB 3 2,527 Mar-30-2021, 06:34 PM
Last Post: buran
  unsupported operand type(s) for /: 'str' and 'int' Error for boxplot soft 1 3,023 Feb-09-2021, 05:40 PM
Last Post: soft

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020