Start .exe file and automatically "press enter for "Save"" in SAS - sas

I have a SAS program I would like to schedule, so we don't have to run it manually.
In the program we call an excecutable (reg.exe), like this:
X CALL "K:\reg.exe";
The executable opens a standard windows save-dialog and all we need to do is press save, which will save an xml-file. The save-dialog already opens in the correct directory.
What I would like is to somehow pass the instruction on, using code, to "press save", so the program can move on and work with the saved xml-file.
Is this possible somehow?
Thanks for your help!

Thomas:
There are numerous Windows utilities for scripted control of an interactive program. For example: AutoIt (my favorite), MouseRobot and AutoHotKey to name a few.
The statement
options xmin noxwait noxsync;
will cause the external program to run, not wait for the command session to exit, and not wait for the command to complete.
If your SAS programs needs to wait for the side effects of the scripting use of reg.exe to occur before occurring, use:
options xmin noxwait xsync;
These settings affect other operating system interfacing features, such as:
X command
CALL SYSTEM routine
%SYSEXEC statement
The use of your reg.exe might require your login to have admin rights.
The SYSTASK statement can also run external commands.
If you find that you are unable to run external commands, it is probably because the system setting NOXCMD is active. This setting is on by default in SAS server environments.

Related

Is there a way to open a new console from a program then cout into it?

I'm trying to have a separate console window for my program that is already in a console. How would I be able to open this new console window then output to that specific console?
I've found ways to do it that work in windows using "cconsolelogger", but not for Linux.
I assume that by "new console", you mean a terminal emulator window.
A terminal is a program like any other, so you start it like any other program. The only standard way in C++ to open another program is std::system, which executes a shell command. Here is an example of opening a terminal emulator:
std::system("xterm");
Note that it is not safe to pass arbitrary user provided input into the command, because it is vulnerable to shell injection.
The POSIX standard - that is followed by Linux operating systems in general - provides other, lower level tools to run another executable. In particular the exec family of functions allows executing another program without starting a sub process and without involving the shell.
Maybe you can use popen.
You can choose the program to write command to it, for example:
gp = _popen("C:\Program Files (x86)\gnuplot\bin\pgnuplot.exe", "w");
fprintf(gp, "set term png\n");

How to print SAS program log to console when running in batch mode

I am running my SAS program in batch mode through windows command prompt.
Start /WAIT "SAS_job" "C:\Program Files\SASHome\SASFoundation\9.4\sas.exe" -sysin D:\MySAS_Test.sas -nosplash -nologo -noicon
Can I display the SAS output or log on command prompt instead of writing to a file? Or print log as SAS program is running to track progress.
I think this is possible in Unix, but I'm not so sure about Windows.
You can write to STDOUT in Unix as mentioned in the documentation. But I don't see any such similar thing in Windows.
The most similar thing is unnamed pipes, which lets you interact with the console - but it's unclear if this is potentially helpful for you or not.
Unfortunately, I suspect SAS is generally not considering Windows a server type environment, and mostly supporting it to allow for desktop use; while it does support Windows servers certainly, most SAS server usage is Linux/Unix.
Your better bet is probably to go in the direction of another program that reads from the log file already produced and writes it out to the console, something analagous to tail in Unix. Or as mentioned in comments open the log in a text editor (you can even 'push' this from SAS if you have option xcmd enabled) and let it auto-refresh periodically.
One common use case in fact is to use, for example, UltraEdit to edit your SAS programs; it can even run them in batch directly, and then retrieve the log in that program.

SAS Error Handling in Windows Batch

I have a scheduled SAS program in Windows, e.g.
sas.exe -nosplash -icon -sysin "myprogram.sas"
This process will hang if there's an "Out of Resources" error (e.g. no disk space), prompting for user input (Retry, Cancel, etc.). As it's a batch job, there is no user to give that input.
Is there a SAS system option which prevents the prompt for user input so it can be dealt with in the code itself?
How about -noterminal?
Extract from documentation:
If NOTERMINAL is specified, dialog boxes are not displayed.
Trying using the option
-batch
Since you are running in batch mode, also consider using the option
-errorabend
The best option is
-get_more_resources ;)
If the program is the cause to the resource error you have another question to ask!

XWait and XSync not behaving correctly

I am running SAS 9 on Windows Vista. I am using system Commands to execute some Python code in some nested macros that are passing different parameters to Python each time. Im using the XSYNC and XWAIT options in SAS.
My understanding of this is that these two options should make SAS pause until the Python script has completed running. With this option the Python Command Prompt window closes when the code completes execution and I am then prompted to type EXIT, which is what you would expect.
However, when I use NOXWAIT XSYNC the SAS code passes each Python invocation, kicks it off and keeps running. After a few minutes I have hundreds of Command Prompt windows open all trying to run at the same time.
What am I doing wrong? As a work around I have tried using x 'EXIT' and call system ('EXIT') to close the windows administrator command prompt window down but this does not seem to be being passed to the window, even though the exit command is in a separate data null step to the one that calls the Python code.
Any ideas?
Here is code I'm using:
data _null_;
x 'cd C:\Python33';
x 'start test.py';
run;
data _null_;
call system ('exit');
run;
What happens is two Command Prompt windows open. The second one is the Python script running. This closes itself and I am left with the administrator window. I have tried putting an x command of 'Exit'; in the first data null step, but this has not worked.
XSYNC tells SAS to execute an external command synchronously, meaning SAS pauses until the command completes. You would use NOXSYNC only in very specific applications.
XWAIT tells SAS to wait for you to explicitly exit the window running the command. In most cases, you would want to use NOXWAIT.
The problem you describe suggests to me that your Python commands are not properly terminated. Sorry, nut I don't know Python. But consider putting your Python statements in a script file, which closes itself with an exit command, or adding an exit command to the SAS call, something like:
data _null_;
call system('python-command;exit');
run;
If this does not help, revise your question and show the complete SAS data step code you are using. Note, using a second X command to push an "exit" statement will not work; it must be part of the initial call.

Using the console in a GUI app in windows, only if its run from a console

My application is a GUI app that has helpful (though optional) information through the terminal (via cout).
In Windows I either have a console appear (by compiling as a console app, or allocating it dynamically) or I don't.
My intention is to make use of the console IF it is being run from the console, but ignore the console completely if it was not. (Essentially what happens in Linux and OS X).
I do not wish to redirect to a file (and in the case of using cin, this is not a viable solution anyway).
Is there a way to attach a GUI app in Windows to the console it is run from, if and only if it is run from a console?
and in the case of using cin, this is not a viable solution anyway
This is the killer detail in your question. It is simple on paper, just first call AttachConsole(ATTACH_PARENT_PROCESS) to try to attach to an existing console. That will fail when your program got started from a GUI program like Explorer or a desktop shortcut. So if it returns FALSE then call AllocConsole() to create your own console.
Using cin is a problem however. The command processor pays attention to your EXE and checks if it is console mode app or a GUI app. It will detect a GUI app in your case and then doesn't wait for the process to complete. It displays the prompt again and waits for input. You will then also wait for input but you'll lose, the command processor got there first. Your output is also intermingled with the command prompt, the easy problem to solve.
There's a simple workaround for that, your user should start your program with start /wait yourapp to tell the command processor to wait for the process to complete. Problem is: nobody ever uses that. And the user will not realize what happens when they type input, intending it to go into your program but it is actually interpreted by the command processor. Producing a mystifying error message or formatting the hard drive.
Only two good ways to solve this unsolvable problem. Either build your program as a console mode app and call FreeConsole() when you find out you want to display a GUI. Or always call AllocConsole(). These are not great alternatives. The first approach is the one used by the Java JVM on Windows. One of the oldest bugs filed against the JVM and driving Java programmers completely batty from the flashing console window.
The third alternative is the only decent one, and the one you don't want, create another EXE that will always use the console. Like Java does, javaw.exe vs java.exe.
A trick is possible, you can rename that file from "yourapp2.exe" to "yourapp.com". It will be picked first when the user types "yourapp" at the command line prompt, a desktop shortcut can still point to "yourapp.exe". Visual Studio uses this trick, devenv.com vs devenv.exe.
You can check CONSOLE_SCREEN_BUFFER_INFO (via GetConsoleScreenBufferInfo) on startup to determine if you've been run from within an existing console. If the buffer's position is 0,0, you were run from outside of the console. For details, see this Microsoft Knowledgebase Article which describes the process.
In order for this to work, you need to compile your application as a console application (using /SUBSYSTEM:CONSOLE), and then detach yourself from the console if the application started a new console (buffer at 0,0). This will cause the program to properly "attach" to the calling console when launched from a command line.
As others have pointed out you have to create a console app and a window app. So, you'd end up with console.exe and app.exe. To make it less obvious at the command-line, you can take advantage of the PATHEXT trick like devenv does. cmd.exe treats a file as a command if its extension is in the PATHEXT environment variable. COM is there by default so you could rename console.exe as app.com, allowing the command app to start the console app attached to the current console.
Note: Of course, a console app can show a GUI if desired.
The difference in the build between app.com and app.exe depends on your build system but it could just be the one attribute that sets the output type. With msbuild (for .vcxproj files), it's just a matter of another build configuration.
you can create an application in console that get a line using argc and prints it;
////
int main(int argc, char *argv[])
{
//here print argv....using cout or printf
}
save the file as console.exe in the folder of your app.
now in your app if you want to see any line in console you can call the command
system("console.exe this is the line i want to print and see in console");