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

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).

https://github.com/savian-net
1 ACCEPTED SOLUTION

Accepted Solutions
AndreasMenrath
Pyrite | Level 9

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

View solution in original post

6 REPLIES 6
AndreasMenrath
Pyrite | Level 9

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

AlanC
Barite | Level 11

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.

https://github.com/savian-net
AlanC
Barite | Level 11
Ahh, never miind, I see what you are saying. Let me get back.
https://github.com/savian-net
AlanC
Barite | Level 11

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;
            }
        }
https://github.com/savian-net
AndreasMenrath
Pyrite | Level 9

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;
ChrisHemedinger
Community Manager

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.

SAS For Dummies 3rd Edition! Check out the new edition, covering SAS 9.4, SAS Viya, and all of the modern ways to use SAS!

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 6 replies
  • 3978 views
  • 1 like
  • 3 in conversation