Hi everyone,
our company has recently started using SAS/CONNECT with BASE SAS 9.4 and I've hit an annoyance that my brain cant let go of!
I'm trying to write a set up program (similar to auto exec,) that sits at the top of your code in an include statement. There's a bit of clean up at the beginning of the program (b/c we're expecting programmers to run their code top to bottom multiple times, or restart from the beginning wanting a "clean sheet" as they de-bug and develop code). After clean up I want the script to sign on to the remote server only if they aren't already signed on. In my head it would look something like this:
*sign on ;
%if <check system macro var or option) %then %do ;
%put NOTE: Already signed on to %sysfunc(getoption(CONNECTREMOTE))" ;
%end;
%else %do ;
%LET session1 = appserv.company.com 1111;
OPTIONS comamid = TCP connectremote = session1 ;
signon session1 sascmd="!sascmd -nosyntaxcheck -noterminal " ;
%end ;
There is no error or warning returned if you signon again to the same server - just a NOTE: to say "you're already signed on here". But I think its good programming practice to have this as a conditional bit of logic in case we have to change the way we sign on at any time down the line.
Maybe I could do it the other way around and force sign-off as part of the clean-up? But that would throw an error! Should I just abandon this or is there something to latch onto that I can use in a conditional statement?
Try this 25 year old (or older) macro from the late great Tom Hoffman.
%macro qrsub
/*----------------------------------------------------------------------
Query SAS/CONNECT status.
----------------------------------------------------------------------*/
(remote /* Optional remote session name */
,wmvar=sysrc /* Macro variable returned to Windows containing
SAS/CONNECT status or UNIX macro variable */
,umvar= /* Optional UNIX macro variable or value used to populate
Windows variable */
);
/*----------------------------------------------------------------------
This code was developed by HOFFMAN CONSULTING as part of a FREEWARE
macro tool set.
-----------------------------------------------------------------------
Usage:
1) check whether active session:
%qrsub;
%if (&sysrc) %then %put no SAS/CONNECT session.;
2) check whether succsesful filename statement:
rsubmit;
filename mymac 'mymacs';
endrsubmit;
%qrsub(mvar=sysfilrc);
%if (&sysrc) %then %put mymacs not available on UNIX server.;
------------------------------------------------------------------------
Notes:
Returns SYSRC equal to 0 if success or 1 if failure.
Use remote parameter when working with multiple SAS/CONNECT seesions.
-----------------------------------------------------------------------
History:
12AUG99 Creation
06SEP00 Fixed bug in remote parameter.
----------------------------------------------------------------------*/
%local str1 str2;
%if (&umvar ^=) %then %let str2 = %str(&)&umvar;
%else %let str2 = 0;
%let &&wmvar = 1;
%let str1 = %str(
rsubmit &remote;)
%nrstr(%sysrput )&wmvar = &str2
%nrstr(;endrsubmit;);
&str1;
%mend qrsub;
Think of the Windows and Unix mentioned in the comments as LOCAL session and REMOTE session.
its a nice bit of code and I understand the logical test - but it doesnt work as I need it to.
I ran this...
%inc "S:\SASCONNECT_test\...\...\qrsub.sas" ;
%qrsub ;
%put &=sysrc. ;
It gave the correct result (sysrc=1 to indicate a failure) but the log gave me an error.
ignore the green warning, we're only on a test license.
I'm tryinig to build a logical step to keep things tidy and avoid log errors and warnings.
If you remember to clear the REMOTE system option after you signoff you can use that as the test.
Try:
%put %sysfunc(getoption(remote));
signon myremote sascmd='!sascmd';
%put %sysfunc(getoption(remote));
signoff;
%put %sysfunc(getoption(remote));
options remote=' ';
%put %sysfunc(getoption(remote));
But that will not detect when the remote session ends on its own (perhaps they rebooted the remote server on you.)
Otherwise once you have a connection make a libref using the REMOTE engine pointing to some place on it. That libref will disappear when the connection is broken so you can use the LIBREF() function to test if the libref is still there to detect if the connection is still there.
thanks, we do assign libraries in the set-up code, so that might be the wisest solution. I'll play about with that, but in the mean time I'll keep the thread open in case anyone else wants to chip in.
thank you Tom!
just as a side note - does the late great Tom Hoffman have a github repo? I love classic code like this - always a gem or two to find from the auld hands
@lindseyj79 wrote:
just as a side note - does the late great Tom Hoffman have a github repo? I love classic code like this - always a gem or two to find from the auld hands
Many have been replaced by enhanced SAS functionality. The %MVARTEST() macro is no longer needed now that %SYMEXIST() function has been created. The %PARSEM() macro is not needed now that you can use %SYSFUNC() to call the COUNTW() function and use the M modifier on the %SCAN() function.
You can find a number of macros based on Tom Hoffman's HCTOOLS package on my GITHUB site. https://github.com/sasutils/macros Such as the updated %MVARTEST() which can now be used to test if a macro variable exists within a specific symbol table.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: