Help using Base SAS procedures

Python

Accepted Solution Solved
Reply
Contributor
Posts: 36
Accepted Solution

Python

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


Accepted Solutions
Solution
‎04-03-2017 09:11 AM
Super User
Super User
Posts: 7,970

Re: Python

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


All Replies
Super User
Posts: 19,822

Re: Python

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

Solution
‎04-03-2017 09:11 AM
Super User
Super User
Posts: 7,970

Re: Python

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.

Contributor
Posts: 36

Re: Python

[ Edited ]

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

Valued Guide
Posts: 505

Re: Python


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;

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 195 views
  • 0 likes
  • 4 in conversation