/* History:
   2025-11-21 Move the IML modules into /macros/internal/iml_modules/v143_modules.sas.
              Set the v143code option to 1 for IML versions 14.3 or 15.1 in %get_v143code().
   2025-12-15 Update %store_IML_version to check for the string "version".
   2025-12-18 Set option set=MKL_DEBUG_CPU_TYPE="5" as the default with versions 14.3 and
              15.1 for efficiency.
   2026-01-16 Set the option MKL_DEBUG_CPU_TYPE if it needs to be set without checking if
              the user has set it.
*/

%macro errorExit(errorCode, msg, exit=1);

  %put ERROR: error code = &errorCode;
  %put ERROR: &msg;
  %if %eval(&exit) %then %do; 
    data _NULL_; abort abend; run;
  %end;

%mend errorExit;

/* SAS macro get get IML version */
%macro store_IML_version(out);

	/* The IML version number can be obtained by calling proc product_status,
		but the info gets sent to the SAS log. So temporarily send the log to an
		external file in the WORK library folder.
	*/

	%local tmpfilename workPath;
  
	/* Create temporary file in the work folder */
	%let workPath    = %sysfunc(pathname(WORK));
	%let tmpfilename = "&workPath./_tmp_file.txt";
	filename fileref &tmpfilename;
  
	/* Redirect the SAS log to the temp file and call proc product_status */
	proc printto log=&tmpfilename new; run;
	proc product_status; run;
	proc printto; run; /* Reset SAS log */
	
	/* Read in the temp file to get the version number. The lines will
		look something like:
		For SAS/IML ...
			Custom version information: 14.3
	*/
	data &out; 
	    length version $ 100;
		keep version;
		retain flag 0;
		infile fileref truncover;
		input x $1000.; 
		x = trim(left(upcase(x)));
		if flag then do;
			if find(x, "VERSION") then do;
				version = scan(x, -1, ' '); /* Get the last token in the string */
				output;
				stop;
			end;
		end;
		/* See if the current line contains "SAS/IML". If it does, then the
			next line will contain the version number at the end of the line. 
			It could be split by a date, so make sure the next line contains "version".
			*/
		val = find(x, "SAS/IML");
		if val then flag = 1;
		delete;
	run;

	%if (%eval(&SYSERR)) %then %do; 
		%errorExit(&SYSERR, determining the default value for the v143code option);
	%end;
  	
	/* Delete the temporary file and clear the file ref */
	data _NULL_;
		rc = filename('fileref', &tmpfilename);
		if fexist('fileref') then rc = fdelete('fileref');
	run;
	filename fileref clear;

%mend store_IML_version;

/* SAS macro to return the default value for the v143code option.
   IML errors can also occur in version 15.1 in addition to 14.3. */
%macro get_v143code(data, var=version);
 
    /* data is the output data set from calling %store_IML_version.
	   This data set should have only one variable called "version". */
 
	%local dsid rc ret ver vnum type;
	%let ret = 0;
	
	%if %sysfunc(exist(&data, DATA)) %then %do;
		%let dsid = %sysfunc(open(&data));
		%let vnum = %sysfunc(varnum(&dsid, &var));
		%if &vnum %then %do;
			%let rc  = %sysfunc(fetchobs(&dsid, 1));                                                                                             
			%let ver = %sysfunc(getvarc(&dsid, &vnum));   
			%put The IML version is &ver;
		%end;
		%let rc = %sysfunc(close(&dsid));  
		/* Search for 14.3 and 15.1 */
		%let rc = %sysfunc(find(&ver, 14.3)) | %sysfunc(find(&ver, 15.1)); 
		%if &rc %then %let ret = 1;
	%end;
	
	/* See if the user has the OS option MKL_DEBUG_CPU_TYPE=5 already set. If so,
		then set ret to 0. */
	%if (&ret) %then %do;
		/* Set the OS option */
		%put Set OS option MKL_DEBUG_CPU_TYPE="5";
		options set=MKL_DEBUG_CPU_TYPE="5";
		%let ret = -1;
		
	%end;
	%put The option 'v143code' will be set to 0;
	
%mend get_v143code;


