BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
kaziumair
Quartz | Level 8

Hi everyone,

 

I am trying to run python code in SAS Viya using proc fcmp. However the output is not getting displayed.

I am using the code from the following link : 

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lecompobjref/p18qp136f91aaqn1h54v3b6pkant.htm

 

proc fcmp;
declare object py(python);
submit into py;
def MyFunc(arg1):
	"Output: MyKey"
	#This is a comment
	ReturnVar = arg1 * 5 #This is another comment
	return ReturnVar
endsubmit;
rc = py.publish();
rc = py.call("MyFunc", 10);
x = py.results["MyKey"];
put x=;
run;

 

Have attached screenshot of the result below for your reference.

Thanks and regards,

Mohammad Umair Kazi

 

python.PNG

1 ACCEPTED SOLUTION

Accepted Solutions
yabwon
Onyx | Level 15

Proc FCMP is "putting" texts/outputs into the Listing ODS, and SAS Studio seems not to have one. 🙂

 

Try to add:

filename t temp;
proc printto print=t;
run;

before the proc FCMP and then add:

proc printto;
run;
data _null_;
  infile t;
  input;
  put _infile_;
run;
filename t;

after the proc FCMP. And look into the log.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



View solution in original post

2 REPLIES 2
yabwon
Onyx | Level 15

Proc FCMP is "putting" texts/outputs into the Listing ODS, and SAS Studio seems not to have one. 🙂

 

Try to add:

filename t temp;
proc printto print=t;
run;

before the proc FCMP and then add:

proc printto;
run;
data _null_;
  infile t;
  input;
  put _infile_;
run;
filename t;

after the proc FCMP. And look into the log.

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Mahult
SAS Employee

Bart is correct. A variation you might try is to create a SAS function that returns the Python function’s value instead of PROC FCMP writing it to an output destination. To do this I added the following:

 

  • A global CMPLIB option to point to the storage location of the FCMP function I’m building.
  • An OUTLIB option on the PROC FCMP statement.
  • A FUNCTION statement to name the SAS function.
  • A module name on the DECLARE statement to be sure any SAS function I build that references a Python function has a unique behind-the-scenes name.
  • The FUNCTION statement's argument name (SASArg) on the CALL method.
  • An FCMP-level RETURN statement to get the final value out of FCMP to the calling program.
  • An ENDSUB statement.

 

options set=MAS_PYPATH="point-to-your-python.exe"

             set=MAS_M2PATH "point-to-your-mas2py.py"

             cmplib=work.fcmp;

 

proc fcmp outlib=work.fcmp.pyfuncs;

function TimesFive(SASArg);

declare object py(python('MySASFunc'));

submit into py;

def MyFunc(arg1):

                "Output: MyKey"

                #This is a comment

                ReturnVar = arg1 * 5 #This is another comment

                return ReturnVar

endsubmit;

rc = py.publish();

rc = py.call("MyFunc", SASArg);

x = py.results["MyKey"];

return(x);

endsub;

run;

 

 

data test;

  x=5;

  y=TimesFive(x);

run;

 

Output:

X     Y

5     25

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 1026 views
  • 1 like
  • 3 in conversation