****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_macros.sas  
*
* Created (mm/dd/yyyy): 12/19/2014
* Last modified: 05/05/2017
* Version: 1.1
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
*   These are utility macros that are used to import files and verify if an dataset contains observations.                                        
*   
*  Program inputs:                                                                                   
*                                     
*  Program outputs:                                                                                                                                       
*                                                                                                  
*  PARAMETERS:    
*	ISDATA(dataset=)
*		dataset = Name of a dataset for which we want to verify if it contains observations
*
*	IMPORTFILES(var=)
*		var = Name of a dataset that we want to import
*            
*  Programming Notes:                                                                                
*                                                                           
*
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO: 
*  Mini-Sentinel Coordinating Center
*  info@mini-sentinel.org
*
*--------------------------------------------------------------------------------------------------
*  CHANGE LOG: 
*
*   Version   Date       Initials      Comment (reference external documentation when available)
*   -------   --------   --------   ---------------------------------------------------------------
*   1.1		  05/05/2017	AP		Added macro %create_comma_charlist
*
***************************************************************************************************;

*Macro to determine whether a dataset is empty or not;
%MACRO ISDATA(dataset=);

%PUT =====> MACRO CALLED: ms_macros v1.0 => ISDATA;

	%GLOBAL NOBS;
	%let NOBS=0;
	%if %sysfunc(exist(&dataset.))=1 and %LENGTH(&dataset.) ne 0 %then %do;
		data _null_;
		dsid=open("&dataset.");
		call symputx("NOBS",attrn(dsid,"NLOBS"));
		run;
	%end;	
%PUT &NOBS.;

%put NOTE: ********END OF MACRO: ms_macros v1.0 => ISDATA ********;

%MEND ISDATA;

%MACRO IMPORTFILES(var=);

%PUT =====> MACRO CALLED: ms_macros v1.0 => IMPORTFILES;

	%IF %INDEX(%UPCASE("&VAR."),CPORT) %THEN %DO;
		proc cimport infile="&infolder.&VAR." library=infolder memtype=data;
		run;
	%END;

%put NOTE: ********END OF MACRO: ms_macros v1.0 => IMPORTFILES ********;

%MEND IMPORTFILES;

%MACRO VAREXIST(DS,VAR);

%PUT =====> MACRO CALLED: ms_macros v1.0 => VAREXIST;

%GLOBAL VAREXIST;
%LET VAREXIST=0;
%LET DSID = %SYSFUNC(OPEN(&DS));
%IF (&DSID) %THEN %DO;
    %IF %SYSFUNC(VARNUM(&DSID,&VAR)) %THEN %LET VAREXIST=1;
    %LET RC = %SYSFUNC(CLOSE(&DSID));
%END;

%put NOTE: ********END OF MACRO: ms_macros v1.0 => VAREXIST ********;

%MEND VAREXIST;

 %macro ms_check_path(path) ;
	%local ln lc d_exist ;
	%let ln = %length(&path) ;
	%if &ln eq 0 %then %do ;
		 %put The parameter path must be non-missing. ;
		 %abort cancel ;
	%end ;
	%else %if &ln > 0 %then %do ;
	   %let path = %sysfunc(translate(&path, %str(/), %str(\))) ;
	   %let lc = %substr(&path, &ln) ;
	   %if "&lc" ne "/" %then %do ;
	   	  %let path = &path./ ;
	   %end ;	   
	   %let d_exist = %ms_dirCheck(&path) ;  
     %if &d_exist eq 0 %then %do ;
        %put Path &path does not exist;
        %abort cancel;
     %end ;	   
	%end ;
	&path 	 /* returns value to calling enivornment, like a function */
%mend ms_check_path ;


%macro ms_dirCheck(dir) ; 
  /* Original Author: Adrien Vallee */	
  /* Original Source: http://www.sascommunity.org/wiki/Tips:Check_if_a_directory_exists */
  /* Terms of Use: http://www.sascommunity.org/wiki/sasCommunity:Terms_of_Use */

   %if %length(&dir) eq 0 %then %do ;
   	  %put The parameter dir must be non-missing.  ;
   	  %abort cancel ;
   %end ;
   %local rc fileref return ; 
   %let rc = %sysfunc(filename(fileref,&dir)) ; 
   %let return = %sysfunc(fexist(&fileref)) ;  
   &return  /* returns value to calling enivornment, like a function */
   %let rc = %sysfunc(filename(fileref)) ;  
%mend ms_dirCheck;


* Macro to collapse overlapping time periods;
%macro MergeTimePeriods(dataset=,DateStart=,DateEnd=);

    proc sort data= &dataset.;
    by PatId &DateStart. &DateEnd.;
    run;

    data &dataset.(rename=New&DateStart.=&DateStart. rename=New&DateEnd.=&DateEnd.);
    set &dataset.;
    by PatId;
    if first.PatId then do;
      New&DateStart. = &DateStart.;
      New&DateEnd. = &DateEnd.;
    end;
    retain New&DateStart. New&DateEnd.;
    if &DateStart. > New&DateEnd. + 1 then do;
      output;
      New&DateStart. = &DateStart.;
      New&DateEnd. = &DateEnd.;
    end;
    else New&DateEnd. = max(&DateEnd.,New&DateEnd.);
    if last.PatId then output;
    keep PatId New&DateStart. New&DateEnd.;
    format New&DateStart. New&DateEnd. date9.;
    run;

%mend MergeTimePeriods;


/*converts space separate list into comma separated list in quotes*/
%macro create_comma_charlist(inlist=, outlist=);
	%global &outlist.;

	%let countvars = %sysfunc(Countw(%quote(&inlist.)));
		%do c = 1 %to &countvars.;
			%let word = %scan(%quote(&inlist),&c.);
				%if &c. = 1 %then %do;
					%let &outlist. = "&word.";
				%end;
				%else %do;
					%let &outlist. = &&&outlist. , "&word.";
				%end;
		%end;
	%let &outlist = %upcase(&&&outlist);
%mend create_comma_charlist;