****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_debug.sas  
*
* Created (mm/dd/yyyy): 12/19/2014
* Last modified: 12/19/2014
* Version: 1.0
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
*   These macros will create dataset copies in a chosen folder for debugging purposes.                                        
*   
*  Program inputs:                                                                                   
*	- dataset(s) to be copied in a debugging folder
* 
*  Program outputs:                                                                                                                                       
*	- dataset(s) copied in a debugging folder
* 
*  PARAMETERS:                                                                       
*	-CreateCopyDir(tmp_libref=)
*		tmp_libref = Name of the library to output dataset copies
*
*	-CopyFile(FileName=,GENMAX=30,suffix=,tmp_libref=);
*		FileName 	= Name of the dataset to be copied
*		suffix 		= suffix to add at the end of the copied file
*		tmp_libref 	= Library where the file will be copied
*
*	-CopyAllFiles(prefix=,suffix=,tmp_libref=);
* 		prefix		= files to be copied will be restricted to theso beginning by the value of prefix
*		suffix 		= suffix to add at the end of the copied file
*		tmp_libref 	= Library where the file will be copied
*
*  Programming Notes:                                                                                
*                                                                           
*
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO: 
*  Sentinel Coordinating Center
*  info@sentinelsystem.org
*
*--------------------------------------------------------------------------------------------------
*  CHANGE LOG: 
*
*   Version   Date       Initials      Comment (reference external documentation when available)
*   -------   --------   --------   ---------------------------------------------------------------
*             mm/dd/yy
*
***************************************************************************************************;


%GLOBAL DEBUG;
%MACRO WRAPPER;
	%IF %SYMEXIST(DEBUG)=0 %THEN %LET DEBUG=0;
%MEND ;
%WRAPPER;

*Creates a debug directory under DPLocal and assign a libname to it;
%macro CreateCopyDir(tmp_libref=);

%put =====> MACRO CALLED: ms_debug v1.0 => CreateCopyDir;

%IF  %SYMEXIST(RunID)=0  	%THEN %LET 	RunID=TMP;
%IF %LENGTH(&tmp_libref.)=0 %THEN  %LET  tmp_libref=&RunID;

	*Check if debugging mode is active;
	%if %eval(&DEBUG.=1) %then %do;

		*Create the debug directory;
		%let Newdir = %sysfunc(DCREATE(&tmp_libref.,&DPLocal.));

		*Assign a libname;
		* libname &tmp_libref. "&DPLocal.&tmp_libref.";	
		libname &tmp_libref. "&DPLocal.&tmp_libref.";

		*Clear the content;
		proc datasets lib=&tmp_libref. nolist nowarn kill memtype=data;
		quit;

	%end;

%put NOTE: ******** END OF MACRO: ms_debug v1.0 => CreateCopyDir ********;

%mend CreateCopyDir;

*Copy a dataset from work to current debug library;

*Copy a dataset from work to current debug library;
%macro CopyFile(FileName=,GENMAX=30,suffix=,tmp_libref=);
	
%put =====> MACRO CALLED: ms_debug v1.0 => CopyFile;

%IF  %SYMEXIST(RunID)=0  	%THEN %LET 	RunID=TMP; 
%IF %LENGTH(&tmp_libref.)=0 %THEN  %LET  tmp_libref=&RUNID;
	*Check if debugging mode is active;
	%if %eval(&DEBUG.=1) %then %do;
		
		*The file to copy must exist;
		%if %sysfunc(exist(&FileName.))=1 %then %do;
	
			%if %sysfunc(exist(&tmp_libref..&FileName.&suffix.))=0 %then 
			%do;
			         DATA &tmp_libref..&FileName.&suffix.(GENMAX=&GENMAX.); 
			           SET &FileName.;
			         RUN;
			%end;
			%else %do;
			    	 DATA &tmp_libref..&FileName.&suffix.; 
			           SET &FileName.;
			         RUN;
			%end;

		%end;
	%end;

%put NOTE: ******** END OF MACRO: ms_debug v1.0 => CopyFile ********;

%mend CopyFile;


*Copy all datasets from work to current debug library. 
 Files to be copied can be restricted to those beginning with a particular prefix;
%macro CopyAllFiles(prefix=,suffix=,tmp_libref=);

%put =====> MACRO CALLED: ms_debug v1.0 => CopyAllFiles;

%IF  %SYMEXIST(RunID)=0  	%THEN %LET 	RunID=TMP;
%IF %LENGTH(&tmp_libref.)=0 %THEN  %LET  tmp_libref=&RUNID;

	*Check if debugging mode is active;
	%if %eval(&DEBUG.=1) %then %do;

		*Get the list of all files in the library;	
		ods output "Library Members" = DataSetsListing;
		proc datasets library=work memtype=data ;
		quit;
		ods output close;

		*Restrict to files with a prefix if necessary;
		%if %LENGTH(&prefix.) ne 0 %then %do;
			data DataSetsListing;
			set DataSetsListing;
			where strip(Name) in: ("&prefix.");
			run;
		%end;

		proc sql noprint;
	        select count(*) 
			into :NumDataSets
	        from DataSetsListing;
	    quit;

		%put &NumDataSets.;

	    *Loop through work datasets and copy them;
	    %let iter=1;
	    %do %while (&iter.<= &NumDataSets.);        
	        data _NULL_;
				*Read only 1 record;
	            set DataSetsListing (firstobs=&iter. obs=&iter.); 

	            *Write the dataset name to a macro variable;           
	            call symput("CurrentDataSet",strip(Name));            
	        run;

	        %CopyFile(FileName=&CurrentDataSet.,suffix=&suffix.,tmp_libref=&tmp_libref.);

	        %let iter=%eval(&iter.+1);
	    %end;

		proc datasets library = work nolist nowarn;
		   delete DataSetsListing;
		quit; 

	%end;

%put NOTE: ******** END OF MACRO: ms_debug v1.0 => CopyAllFiles ********;

%mend CopyAllFiles;