Hi,
I have a number of python scripts saved on my c drive. The scripts are currently ran with Selenium web driver. Is it possible to be able to execute py scripts (with selenium) in SAS? If so, do you have any code that I could use to do this? At the moment I have to double click each PY script to execute Selenium.
Thanks
Chris
I haven't heard of this Selenium. However if you are able to, from Windows explorer, double click to open/run then that means you have associated the python files to be automatically opened with this Selenium, and that runs them. You can emulate this behaviour by sending out Operating System (assuming it is allowed) commands from SAS using: call system, or x "..."; For example to open abc.txt with notepad I could do:
x '"c:\programs\notepad.exe" "c:\documents\abc.txt"';
Note the single and double quotes. the part between ' and ' gets sent to the OS - so has to conform to those rules. The above means open the executable file call notepad.exe located in the given path, and that accepts a filename of a file to open. It is different command line for all applications, but the process of generating the commands from SAS is the same.
I would also question why you need to use both, i.e. why run python programs from SAS. Doesn't seem particularly efficient. Why not have one Python file which calls all the other programs, hence reduce your App overhead to one, and use one tech.
Do you know the command line string to execute your Python script? If so, you can use the SYSEXEC or X command statement.
I haven't heard of this Selenium. However if you are able to, from Windows explorer, double click to open/run then that means you have associated the python files to be automatically opened with this Selenium, and that runs them. You can emulate this behaviour by sending out Operating System (assuming it is allowed) commands from SAS using: call system, or x "..."; For example to open abc.txt with notepad I could do:
x '"c:\programs\notepad.exe" "c:\documents\abc.txt"';
Note the single and double quotes. the part between ' and ' gets sent to the OS - so has to conform to those rules. The above means open the executable file call notepad.exe located in the given path, and that accepts a filename of a file to open. It is different command line for all applications, but the process of generating the commands from SAS is the same.
I would also question why you need to use both, i.e. why run python programs from SAS. Doesn't seem particularly efficient. Why not have one Python file which calls all the other programs, hence reduce your App overhead to one, and use one tech.
I agree with your final point. Many thanks for your thoughts
invocation
This opens a dialog box with selection HEIGHT when you click
on HEIGHT the text HEIGHT is returned to SAS
%utl_submit_py64('
from Tkinter import *;
master = Tk();
var = StringVar(master);
var.set("Missing Variables");
option = OptionMenu(master, var, "HEIGHT");
option.pack();
def ok():;
. print(var.get());
. master.quit();
button = Button(master, text="OK", command=ok);
button.pack();
mainloop();
f = open("d:/txt/myfile.txt", "w");
f.write(var.get());
f.close();
');
%utl_submit_py64('
from Tkinter import *;
master = Tk();
var = StringVar(master);
var.set("Missing Variables");
option = OptionMenu(master, var, &nams);
option.pack();
def ok():;
. print(var.get());
. master.quit();
button = Button(master, text="OK", command=ok);
button.pack();
mainloop();
f = open("d:/txt/myfile.txt", "w");
f.write(var.get());
f.close();
');
%macro utl_submit_py64(pgm)/des="Semi colon separated set of py commands";
* write the program to a temporary file;
filename py_pgm "%sysfunc(pathname(work))/py_pgm.py" lrecl=32766 recfm=v;
data _null_;
length pgm $32755 cmd $1024;
file py_pgm ;
pgm=&pgm;
semi=countc(pgm,';');
do idx=1 to semi;
cmd=compbl(cats(scan(pgm,idx,';')));
if cmd=:'.' then cmd=substr(cmd,2);
put cmd $char384.;
putlog cmd $char384.;
end;
run;
run;quit;
%let _loc=%sysfunc(pathname(py_pgm));
%put &_loc;
filename rut pipe "C:\Python_27_64bit/python.exe &_loc";
data _null_;
file print;
infile rut;
input;
put _infile_;
run;
filename rut clear;
filename py_pgm clear;
%mend utl_submit_py64;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.