****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_caresettingprincipal.sas  
*
* Created (mm/dd/yyyy): 07/31/2014
* Last modified: 04/21/2016
* Version: 1.1
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
* 	This program will expand the aggregated caresettingprincipal such that it can be used to extract
*  	claims in the MSCDM.  This macro will also process wilcards.
*
*  Program inputs:                                                                                   
*	-dataset containing the caresettingprincipal to expand
* 
*  Program outputs:                                                                                                                                       
*	-dataset with the expanded caresettingprincipal
* 
* PARAMETERS:
*  	-InFile	= Name of the dataset containing the caresettingprincipal to expand
*  	-Var	= Name of the varaible containing the caresettingprincipal variable
*  	-OutFile= Name of the dataset with the expanded caresettingprincipal
*
*  Programming Notes:                                                                                
*                                                                           
*
*--------------------------------------------------------------------------------------------------
* CONTACT INFO:
*  Sentinel Coordinating Center
*  info@sentinelsystem.org
*
*--------------------------------------------------------------------------------------------------
*  CHANGE LOG: 
*
*   Version   Date       Initials	   Comment (reference ex ternal documentation when available)
*   -------   --------   --------   ---------------------------------------------------------------
*     1.1     04/21/2016   DM		The period is now a valid PDX no matter what enctype is.
*
***************************************************************************************************;

%MACRO ms_caresettingprincipal(InFile=, Var=, OutFile=);

%put =====> MACRO CALLED: ms_caresettingprincipal v1.1;

    %let message=;

	data &OutFile.;
	set &InFile;
	format EncType $2.PDX $1.;

	* Translate the missing value (dot) into an underscore and the wildcard (star) into an A to avoid problems later;
   	&Var. = translate(&Var.,'A','*','_','.');
   	&Var. = upcase(&Var.);

	* All Caresetting and Principal;
	if indexw(&var.,'AAA') or &var. eq "" then do;
		EncType="**"; PDX="*"; output;		
	end;
	else do;
      Numarg=length(compress(&var.," '"))/3;
      do i=1 to Numarg;	  	 
         EncType=substr(compress(scan(&var.,i),"'"),1,2);
		 if EncType="AA" then EncType="**";			* Wildcard for Caresetting;
		 PDX=substr(compress(scan(&var.,i),"'"),3,1);		
       
        if EncType in ("ED","AV","OA") and PDX ne "A" then do;
            call symput('message', "WARNING: In &var., at least one unusual lookup value for PDX was used with ED and/or AV and/or OA");
        end;

		 if PDX="_" then PDX=" ";					* The underscore was used for missing PDX;
		 if PDX="A" then PDX="*";					* Wildcard for PDX;

		 output;
      end;
   	end;
	drop i Numarg;
	run;

%put &message.;

%put NOTE: ********END OF MACRO: ms_caresettingprincipal v1.1********;

%MEND ms_caresettingprincipal;