/******************************************************************************
 * Programme : Clean Up "Ghost" Sessions: A Safe Macro to Terminate Stuck SAS Processes on Windows
 * Reference : KILLSEEBD4
 * Source    : https://www.wearecas.eu/en/sampleCode/KILLSEEBD4
 ******************************************************************************/

/* --- BLOC 1 --- */
%macro killsess(sessno);
  %local err;
  %let err=ERR%str(OR);

  %if not %length(&sessno) %then %let sessno=1;
  %else %if %length(%sysfunc(compress(&sessno,1234567890))) %then %do;
    %put &err: (killsess) You must specify an integer for the session number sessno=&sessno;
    %goto exit;
  %end;

/* --- BLOC 2 --- */
x taskkill /f /fi "USERNAME eq &sysuserid" /fi "PID ne &sysjobid" /fi "WINDOWTITLE eq SAS Session &sessno.*";

/* --- BLOC 3 --- */
  %goto skip;
  %exit: %put &err: (killsess) Leaving macro due to problem(s) listed;
  %skip:
%mend killsess;

