If I start a SAS Workspace object, I can name it:
var ws = new SAS.Workspace{Name = "SASWS_0"};
Then, if I need to search through the processes running on the machine, I can find all instances of SAS using the following:
var sasProcs = Process.GetProcessesByName("sas");
If sasProcs has 10 entries, how can I get the one associated with the ws object? Currently, I need to check the process MainWindowHandle to check if it is a 0 to find the workspace but I don't think that is the best way to handle it (pun intended).
Hi Alan,
i would query the workspace for the automatic macro variable "SYSJOBID". This is the process id of your SAS process. With the PID it is easy to find that process in your operating system.
Best Regards
Andreas
Hi Alan,
i would query the workspace for the automatic macro variable "SYSJOBID". This is the process id of your SAS process. With the PID it is easy to find that process in your operating system.
Best Regards
Andreas
Thanks Andreas. When I start the Workspace, how do I know what PID it is running under? If I have the process Windows, how do I get to the Workspace object? Either one should allow the meeting of the 2 but I am missing something here. I may have it in some old code but have not searched through everything.
There is probably a better way but this worked. I wrote an extension method in case it is needed:
public static Dictionary<string, string> GetAutoVars(this Workspace ws)
{
try
{
var lang = ws.LanguageService;
lang.LineSeparator = "\r\n";
var code = string.Join(Environment.NewLine, "OPTIONS PAGESIZE=MAX; %PUT --BEGIN--; %PUT _ALL_; %PUT --END--;");
lang.Submit(code);
#pragma warning disable CS0219
const LanguageServiceCarriageControl cc = new LanguageServiceCarriageControl();
const LanguageServiceLineType lt = new LanguageServiceLineType();
#pragma warning restore CS0219
lang.FlushLogLines(100, out Array carriage, out Array lineTypes, out Array lines);
var lineList = lines.OfType<string>().ToList();
var start = lineList.IndexOf("--BEGIN--");
var finish = lineList.IndexOf("--END--");
var recs = lineList.Skip(start + 1).Take(finish - start - 1).Select(p => p.Replace("AUTOMATIC", string.Empty).Trim().Split(' ', 2)).ToList();
var autoVars = recs.ToDictionary(p => p[0].ToString(), p => p.Length == 2 ? p[1]?.ToString() : null);
return autoVars;
}
catch (Exception ex)
{
throw ex;
}
}
Hi Alan, i guess you also query your SAS Workspace for data. With the SAS OleDB Driver it is just a simple SQL-query against the SAS internal dictionary tables (also available in the SASHELP Library):
select name, value from dictionary.macros;
or
select name, value from sashelp.vmacro;
There is another way to get this without submitting code or querying for macro variables.
The process ID is among the values available from IHostSystem, which you can get to from IUtilities, which you can get from the Workspace.
// -- given a SASWorkspace ws //----- get utilities interface IUtilities iUtil=ws.Utilities; //----- get HostSystem interface IHostSystem iHostSystem=iUtil.HostSystem; //----- get SAS version, process ID, and other info - in softwareInfo array Array softwareInfo, hardwareInfo; iHostSystem.GetInfo(out softwareInfo, out hardwareInfo);
Some secrets for decoding are in Peter Eberhardt's paper.
This would probably perform faster...even if it's a little convoluted.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.