DATA Step, Macro, Functions and more

How to get the command that started a Windows batch session?

Accepted Solution Solved
Reply
PROC Star
Posts: 1,435
Accepted Solution

How to get the command that started a Windows batch session?

I'm working in windows, and some other program will start a batch SAS job by running a command like:

  sas.exe -sysin "d:\someprogram.sas" -log "d:\logs\mylog.log"  [...]

 

I have two questions:

1. From inside the SAS job, is there a way I can find the full command string that was used to start SAS?  I don't see it in a macro variable. Perhaps it's available in an environment variable somewhere?

 

2. From inside the SAS job, is there a way I can find the value that was passed for the -log argument?  I want to be able to scan the log for errors etc.  I want to allow the program that calls SAS to decide where the log should go, so they can pass any valid location in the -log parameter and the SAS code will know where the log is.

 

If it's not possible, I can think about some workarounds (e.g. add -SYSPARM to the command that starts SAS, and use it to pass the log location). 

 

Thanks,

--Q.

 

 

 


Accepted Solutions
Solution
‎10-10-2017 09:46 AM
SAS Employee
Posts: 26

Re: How to get the command that started a Windows batch session?

Quentin,

 

1. I do not believe that from inside the SAS job you are able to find the command string that was used to start SAS. I could be wrong, but I am not aware that this is possible. 

 

2. To tell the program to place the SAS Log in a file, use the following command in the Batch file: 

 

"C:\Program Files\SASHome\SASFoundation\9.4\sas.exe" -SYSIN "C:\Users\<yourUserName>\Documents\ ... PROGRAM.sas" -LOG "C:\Users\<yourUserName>\Documents\ ... PROGRAM.log" -PRINT "C:\Users\<yourUserName>\Documents\ ... PROGRAM.lst" -ICON -NOSPLASH

 

The above text should all be one one line of your Batch file. The bold text indicates this is where you should place the location of your log files. Also, if you are using an ODS statement in your SAS Program be sure to use an ODS Listing statement as well, with the desired location of your log file:

 

ods listing gpath="C:\Users\<yourUserName>\Documents\ ... ";

 

This should allow you to print a log file and any graphs or tables that you have created to a file location using a Batch command. Let me know if you have any questions!

View solution in original post


All Replies
PROC Star
Posts: 1,435

Re: How to get the command that started a Windows batch session?

Looks like for part two, I can just use:

%put %sysfunc(getoption(log));

Solution
‎10-10-2017 09:46 AM
SAS Employee
Posts: 26

Re: How to get the command that started a Windows batch session?

Quentin,

 

1. I do not believe that from inside the SAS job you are able to find the command string that was used to start SAS. I could be wrong, but I am not aware that this is possible. 

 

2. To tell the program to place the SAS Log in a file, use the following command in the Batch file: 

 

"C:\Program Files\SASHome\SASFoundation\9.4\sas.exe" -SYSIN "C:\Users\<yourUserName>\Documents\ ... PROGRAM.sas" -LOG "C:\Users\<yourUserName>\Documents\ ... PROGRAM.log" -PRINT "C:\Users\<yourUserName>\Documents\ ... PROGRAM.lst" -ICON -NOSPLASH

 

The above text should all be one one line of your Batch file. The bold text indicates this is where you should place the location of your log files. Also, if you are using an ODS statement in your SAS Program be sure to use an ODS Listing statement as well, with the desired location of your log file:

 

ods listing gpath="C:\Users\<yourUserName>\Documents\ ... ";

 

This should allow you to print a log file and any graphs or tables that you have created to a file location using a Batch command. Let me know if you have any questions!

PROC Star
Posts: 1,435

Re: How to get the command that started a Windows batch session?

Posted in reply to danielduval0

Thanks @danielduval0.  That's a bummer about #1.  I would think maybe someone with fancy windows skills could find a way to get the information back from windows somehow.  Something like using the process ID or whatever of the SAS session to look up that command that started that process.  But maybe it'd be too messy to be worthwhile.

 

Understand how to do #2 to direct the log somewhere.  But in this case, the non-SAS application is allowing the user to specify some of the options that are ultimately used to generate the command to start SAS.  So for example, they will choose where to put the log, and perhaps other options for debugging etc.  At least with %GETOPTION(log) I can get the log location.  But would be nice to be able to get the whole command string.

 

Since  they are generating the command string anyway, I might just ask them to pass the generated command string in -sysparm, assuming they can work out the quote marks.

Super User
Posts: 3,777

Re: How to get the command that started a Windows batch session?

If you wrote a shell script to invoke the batch job, you could assign the SAS command to an environment variable then use the SYSGET function in SAS to read it. 

Regular Contributor
Posts: 195

Re: How to get the command that started a Windows batch session?

Is xcmd enabled? Powershell can be used to get the pid of the parent-process. See: https://superuser.com/questions/884785/how-can-i-get-full-command-and-parent-process-id-from-powersh...
☑ This topic is solved.

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

Discussion stats
  • 5 replies
  • 139 views
  • 3 likes
  • 4 in conversation