Published on :
Administration CREATION_INTERNE

Killsess - SAS Windows Session Termination

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
This macro is designed for Windows environments (like Citrix) where a SAS© session may remain stuck. It allows terminating another SAS© session of the current user using the Windows utility 'taskkill'. It includes a safety measure to prevent terminating the SAS© session that is executing the macro (PID verification via &sysjobid).
Data Analysis

Type : CREATION_INTERNE


The script does not use any external data or SAS tables. It relies entirely on the execution of system commands (CMD) via the X statement.

1 Code Block
MACRO DEFINITION
Explanation :
Macro definition and parameter validation. Checks that the session number (&sessno) is a valid integer. Initializes the local variable for error messages.
Copied!
1%macro killsess(sessno);
2 %local err;
3 %let err=ERR%str(OR);
4 
5 %IF not %LENGTH(&sessno) %THEN %let sessno=1;
6 %ELSE %IF %LENGTH(%sysfunc(compress(&sessno,1234567890))) %THEN %DO;
7 %put &err: (killsess) You must specify an integer for the SESSION number sessno=&sessno;
8 %goto exit;
9 %END;
2 Code Block
SYSTEM COMMAND
Explanation :
Execution of the Windows 'taskkill' command via the X statement. The command forces the termination (/f) of processes filtered (/fi) by username and window title (corresponding to the session number), while explicitly excluding the PID of the current session (&sysjobid) to prevent self-termination.
Copied!
1x taskkill /f /fi "USERNAME eq &sysuserid" /fi "PID ne &sysjobid" /fi "WINDOWTITLE eq SAS
2Session &sessno.*";
3 
3 Code Block
MACRO EXIT
Explanation :
Macro exit management. Jump labels for error handling and end of macro definition.
Copied!
1 %goto skip;
2 %exit: %put &err: (killsess) Leaving macro due to problem(s) listed;
3 %skip:
4%mend killsess;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : This is public domain software. No guarantee as to suitability or accuracy is given or implied. User uses this code entirely at their own risk. Author: Roland Rashleigh-Berry.


Banner
Expert Advice
Expert
Simon
Expert SAS et fondateur.
« The killsess macro addresses a common frustration in shared Windows environments (like Citrix or Remote Desktop): the "frozen" or "ghost" SAS session that refuses to close. By leveraging the native Windows taskkill utility, this script empowers users to clean up their own workspace without needing IT intervention, while maintaining essential safety protocols.

Strategic Best Practices
PID Safety Mechanism: The use of the automatic macro variable &sysjobid is brilliant. By including the filter /fi "PID ne &sysjobid", the macro ensures it does not "commit suicide" by killing the very session that is running the command. This allows the cleanup process to complete successfully and log its results.

Precision Targeting: Filtering by both USERNAME and WINDOWTITLE provides a multi-layered safety net. It prevents you from accidentally terminating a colleague's session or closing a different instance of your own SAS sessions (e.g., Session 2) that might be performing a critical long-running calculation.

Administrative Requirements: Like any call to the system shell via the X statement, this macro requires the XCMD system option to be enabled. Furthermore, because taskkill is a Windows-specific executable, this macro will fail in Linux-based environments (like SAS Viya). »