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 INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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