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

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

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

View solution in original post

4 REPLIES 4
Reeza
Super User

Do you know the command line string to execute your Python script? If so, you can use the SYSEXEC or X command statement. 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

cmoore
Obsidian | Level 7

I agree with your final point. Many thanks for your thoughts

rogerjdeangelis
Barite | Level 11

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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

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
  • 4 replies
  • 1846 views
  • 0 likes
  • 4 in conversation