****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_CreateNumCat.sas  
*
* Created (mm/dd/yyyy): 01/06/2015
* Last modified: 01/06/2015
* Version: 1.0
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
*   This program creates a "Cat" variable based on a variable value and the possible values or 
*	ranges of values it can take. 
*   
*  Program inputs:                                                                                   
*	-SAS data file containing the variable for which we want to associate a "Cat" value       
* 
*  Program outputs:                                                                                                                                       
*	-SAS data file with the "Cat" variable created
* 
*  PARAMETERS:                                                                       
*	-infile		= Name of SAS dataset containing the variable for which we want to associate a "Cat" value  
*   -outfile	= Name of SAS dataset data file with the "Cat" variable created 
*	-var 		= Name of the variable for which we want to associate a "Cat" value (e.g.: year)          							  
*	-catstring  = string containg the possible values or ranges of values for var (e.g.: 2006-2008 2009-2012)
*	-fmt		= number of characters to allow to the "Cat" variable that will be created
* 
*  Programming Notes:                                                                                
*                                                                           
*
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO: 
*  Sentinel Coordinating Center
*  info@sentinelsystem.org
*
*--------------------------------------------------------------------------------------------------
*  CHANGE LOG: 
*
*   Version   Date       Initials      Comment (reference external documentation when available)
*   -------   --------   --------   ---------------------------------------------------------------
*             mm/dd/yy
*
***************************************************************************************************;

%macro ms_CreateNumCat(infile=,
                       outfile=,
                       var=,
                       catString=,
                       fmt=20);

%put =====> MACRO CALLED: ms_CreateNumCat v1.0;

	%global NumCat;
	%let NumCat=0;
	%let fmtSTR=;

    data _NULL_;
    format fmtSTR $400.;  * At least 3 times the max length for CATEGORIZATION;
    NumCat=countw("&CATEGORIZATION."," ");
    do i=1 to NumCat;
        fmtSTR=strip(fmtSTR)||" "||strip(scan("&catString.",i," "))||"='"||strip(scan("&catString.",i," "))||"'";
    end;
    call symputx("fmtSTR",fmtSTR);
    call symputx("NumCat",NumCat);
    run;
    %put &NumCat. &fmtSTR.;

	proc format fmtlib;
	value &var.Fmt
	&fmtSTR;
	run;

	data &outfile.;
	set &infile.;
	format Cat $&fmt..;
	Cat=put(&var.,&var.Fmt.);
	run;

	%put NOTE: ******** END OF MACRO: ms_CreateNumCat v1.0 ********;

%mend ms_CreateNumCat;