****************************************************************************************************
*                                           PROGRAM OVERVIEW
****************************************************************************************************
*
* PROGRAM: ms_cidatablest5.sas  
*
* Created (mm/dd/yyyy): 06/28/2017
* Last modified: 03/27/2018
* Version: 1.3
*
*--------------------------------------------------------------------------------------------------
* PURPOSE:
*   This macro will create the output tables for type 5 cohort                                       
*   
*  Program inputs:    
*	-infolder.&cohortfile.
*	-_PtsMasterList
*	-_Dispensings 
*                                     
*  Program outputs:  
*	-MSOC.&RUNID._t5_CIDA_disp_by_daysupp
*	-MSOC.&RUNID._t5_CIDA_firsteps
*	-MSOC.&RUNID._t5_CIDA_alleps
*	-MSOC.&RUNID._t5_CIDA_episode_duration
*	-MSOC.&RUNID._t5_CIDA_episdur_censor
*	-MSOC.&RUNID._t5_CIDA_gaps
*                                                                                                  
*  PARAMETERS:                                                                       
*            
*  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		  10/10/17	 	AP		Added geographic stratifications
*
*   1.2       03/14/18      RR		Changed Group length to $40 (QRP-495)
*
*   1.3       03/27/18      AP		Added EpisodeLength per Episode to t5_cida_episdur (QRP-516)
*
***************************************************************************************************;

%macro ms_cidatablest5();

%put =====> MACRO CALLED: ms_cidatablest5 v1.3;

    *creating age group dataset;
    data _age;
    numgrp = countw("&AGESTRAT."," ") ;
    do i=1 to numgrp;
        AgeGroup=scan("&AGESTRAT",i," ");
        AgeGroupNum=i;
        output;
    end;
    keep AgeGroup AgeGroupNum;
    run;

    /****************************************************************/
    /* Squaring (or making sure all possible values are represented */
	/* Note: Continuous values are not squared						*/
    /****************************************************************/
    data _AgeSex;
    set infolder.&cohortfile.(where=(strip(group)="&itgroup.") drop=sex);
	format sex $1.;	
    format AgeGroup $9.;
    numgrp = countw("&AGESTRAT."," ") ;
    totrxsup=0; episodedispensing=0; disp_rxsup=0;
    do i=1 to numgrp;
        AgeGroup=scan("&AGESTRAT",i," ");
        AgeGroupNum=i;
	    sex="F"; output;
	    sex="M"; output;
	    sex="O"; output;
    end;
    keep group AgeGroup AgeGroupNum sex totrxsup episodedispensing disp_rxsup;
    run;

	%if "&race_out" = "Y" %then %do;
	  	data _race;
	     	format race $1.;
	        do i=0 to 5;
	            race=i;
	            output;
	        end;
	    run;

       proc sql noprint  undo_policy=none;
        create table _AgeSex as
        select base.*,
               com.race
        from  _AgeSex as base,
              _race as com;
        quit;
	%end;
	%if "&hispanic_out" = "Y" %then %do;
	  	data _hispanic;
	     	format hispanic $1.;
	            hispanic="N";
	            output;
				hispanic="Y";
				output;
				hispanic="U";
				output;
	    run;

       proc sql noprint undo_policy=none;
        create table _AgeSex as
        select base.*,
               com.hispanic
        from  _AgeSex as base,
              _hispanic as com;
        quit;
	%end;

	/*Geographic strata*/
	%if %str(&GEOG.) ne %str() & %length(&GEOG.) > 0. %then %do;	
		%macro squaregeog();
			%let num_geog = %sysfunc(countw(&geog));
			%put &num_geog;

			data _uncertain;
				zip_uncertain='N'; output;
				zip_uncertain='Y'; output;
			run;

			proc sql noprint undo_policy=none;
				create table _AgeSex as
				select base.*,
					   com.zip_uncertain
				from  _AgeSex as base,
					  _uncertain as com;
			quit;

			%do g = 1 %to %eval(&num_geog);
				%let geog_stratification = %scan(&geog., &g.);
					%put Squaring &geog_stratification.;
					%if "&geog_stratification" = "ZIP3" %then %do; %let select = substr(zip,1,3) as zip3; %end;
					%if "&geog_stratification" = "STATE" %then %do; %let select = statecode as state; %end;
					%if "&geog_stratification" = "HHS_REG" %then %do; %let select = hhs_region as hhs_reg; %end;
					%if "&geog_stratification" = "CB_REG" %then %do; %let select = cb_region as cb_reg; %end;

				proc sql noprint;
					create table _geogcodes as
					select distinct &select. format=$7. length=7
					from infolder.&zipfile.;
				quit;

				*Add Missing and Invalid rows;
	 			data _geogcodes;
					set _geogcodes end=eof;
				   	format &geog_stratification. $7.;
					output;
					if eof then do;
			        	&geog_stratification.='Missing';
					 	output;
						&geog_stratification.='Invalid';
						output;
						%if "&geog_stratification" = "HHS_REG" | "&geog_stratification" = "CB_REG" %then %do;
						&geog_stratification.='Other';
						output;
						%end;
					end;
		      	run;

				proc sql noprint undo_policy=none;
			        create table _AgeSex as
			        select base.*,
			               com.&geog_stratification.
			        from  _AgeSex as base,
			              _geogcodes as com;
		        quit;

			   	proc datasets nowarn noprint lib=work; 
					delete _uncertain _geogcodes;
				quit;
			%end;
		%mend;
		%squaregeog();
	%end;

	/*Episode-level data*/
	proc sql noprint;
	    create table _alleps as 
	    select mstr.*,
	           age.AgeGroupNum
	    from  _PtsMasterList as mstr left join
	          _age as age
	    on age.AgeGroup=mstr.AgeGroup;
	quit;

	data _alleps(rename=tte=episodelength);
		set _alleps(in=a)
	        _AgeSex(drop=disp_rxsup in=b);
	    if a then do;
	        patient=1;
	    end;
	    else do;
	        patient=0;    
	        episode_cens_elig = 0;
			episode_cens_dth = 0;
			episode_cens_dpend = 0;
			episode_cens_qryend = 0;
			episode_cens_episend = 0;	
			episode_cens_spec = 0;
	    end;
	run;

	/*Dispensing level data*/
	proc sql noprint;
	    create table _disp as 
	    select disp.*,
	           age.AgeGroupNum
	    from  _dispensings as disp left join
	          _age as age
	    on age.AgeGroup=disp.AgeGroup;
	quit;

	data _disp(rename=disp_rxsup=daysupp);
		set _disp(in=a)
	        _AgeSex(drop=totrxsup episodedispensing in=b);
	    if a then do;
	        dispensing=1;
	    end;
	    else do;
	        dispensing=0;    
	    end;
	run;


	/****************************/
	/* t5_cida_disp_by_daysupp	*/
	/****************************/
    %macro create_t5_cida_disp_by_daysupp(class=,outfile=,level=);
        proc means data=_disp nway noprint missing;
	        var dispensing;
	        class &class.;
	        id dispensing;
	        output out=&outfile.(drop=_:) sum(dispensing)=Dispensings;                                  
        run;

	    %if %length(&class.)=0 %then %do;
	    	data _t5_cida_disp_by_daysupp;
	            set &outfile.(drop=dispensing);
	            format level $3. group $40.;
	            Level="&level.";
				group = "&ITGROUP.";
	    	run;
		%end;
	    %else %do;
	    	data _t5_cida_disp_by_daysupp;
	        	set _t5_cida_disp_by_daysupp
	                &outfile.(drop=dispensing in=a);
	                if a then do;	
						Level="&level.";
						group = "&ITGROUP.";
					end;
			run;
		%end;

		proc datasets lib=work noprint nowarn; delete &outfile.; quit;
    %mend;

 	*Overall;
    %create_t5_cida_disp_by_daysupp(class=,outfile=_ALL,level=000);

	*Sex and Age Group;
	%create_t5_cida_disp_by_daysupp(class=Sex,outfile=_S,Level=002);
    %create_t5_cida_disp_by_daysupp(class=AgeGroup AgeGroupNum,outfile=_AG,Level=003);
	%create_t5_cida_disp_by_daysupp(class=Sex AgeGroup AgeGroupNum,outfile=_SAG,Level=004);

	*Day Supply;
	%create_t5_cida_disp_by_daysupp(class=daysupp,outfile=_DSup,Level=580);
	%create_t5_cida_disp_by_daysupp(class=daysupp sex,outfile=_DSupS,Level=582);
	%create_t5_cida_disp_by_daysupp(class=daysupp AgeGroup AgeGroupNum,outfile=_DSupAG,Level=583);
	%create_t5_cida_disp_by_daysupp(class=daysupp sex AgeGroup AgeGroupNum,outfile=_DSupSAG,Level=584);

	*Race Tables;
	%if "&race_out" = "Y" %then %do;
 	 	%create_t5_cida_disp_by_daysupp(class=race,outfile=_R,Level=110);
 		%create_t5_cida_disp_by_daysupp(class=race sex,outfile=_R_S,Level=111);
 		%create_t5_cida_disp_by_daysupp(class=race agegroup agegroupnum,outfile=_RAG,Level=112);
		%create_t5_cida_disp_by_daysupp(class=race daysupp,outfile=_RDSup,Level=585);
	%end;

	*Hispanic Tables;
	%if "&hispanic_out" = "Y" %then %do;
 	 	%create_t5_cida_disp_by_daysupp(class=hispanic,outfile=_H,Level=115);
 		%create_t5_cida_disp_by_daysupp(class=hispanic sex,outfile=_H_S,Level=116);
 		%create_t5_cida_disp_by_daysupp(class=hispanic agegroup agegroupnum,outfile=_HAG,Level=117);
		%create_t5_cida_disp_by_daysupp(class=hispanic daysupp,outfile=_HDSup,Level=586);
	%end;

	*Geographic Tables;
	%if %str(&GEOG.) ne %str() & %length(&GEOG.) > 0. %then %do;	
		%create_t5_cida_disp_by_daysupp(class=zip_uncertain, outfile = _ZU, level=060);
		%create_t5_cida_disp_by_daysupp(class=zip_uncertain sex, outfile = _ZU_S, level=061);
		%create_t5_cida_disp_by_daysupp(class=zip_uncertain agegroup agegroupnum, outfile = _ZU_AG, level=062);

		%if %index(&geog.,ZIP3) > 0 %then %do;
			%create_t5_cida_disp_by_daysupp(class=zip3, outfile = _ZIP3, level=020);
			%create_t5_cida_disp_by_daysupp(class=zip3 zip_uncertain, outfile = _ZIP3_UN, level=021);
			%create_t5_cida_disp_by_daysupp(class=zip3 sex, outfile = _ZIP3_S, level=022);
			%create_t5_cida_disp_by_daysupp(class=zip3 sex zip_uncertain, outfile = _ZIP3_S_UN, level=023);
			%create_t5_cida_disp_by_daysupp(class=zip3 agegroup agegroupnum , outfile = _ZIP3_AG, level=024);
			%create_t5_cida_disp_by_daysupp(class=zip3 agegroup agegroupnum zip_uncertain, outfile = _ZIP3_AG_UN, level=025);

			*Race/Hispanic Tables;
			%if "&race_out" = "Y" %then %do;
		 	 	%create_t5_cida_disp_by_daysupp(class=zip3 race, outfile = _ZIP3_R, level=028);
				%create_t5_cida_disp_by_daysupp(class=zip3 race zip_uncertain, outfile = _ZIP3_R_UN, level=029);
			%end;
			%if "&hispanic_out" = "Y" %then %do;
		 	 	 %create_t5_cida_disp_by_daysupp(class=zip3 hispanic, outfile = _ZIP3_H, level=030);
				%create_t5_cida_disp_by_daysupp(class=zip3 hispanic zip_uncertain, outfile = _ZIP3_H_UN, level=031);
			%end;
		%end;

		%if %index(&geog.,STATE) > 0 %then %do;
			%create_t5_cida_disp_by_daysupp(class=state, outfile = _state, level=040);
			%create_t5_cida_disp_by_daysupp(class=state zip_uncertain, outfile = _state_UN, level=041);
			%create_t5_cida_disp_by_daysupp(class=state sex, outfile = _state_S, level=042);
			%create_t5_cida_disp_by_daysupp(class=state sex zip_uncertain, outfile = _state_S_UN, level=043);
			%create_t5_cida_disp_by_daysupp(class=state agegroup agegroupnum , outfile = _state_AG, level=044);
			%create_t5_cida_disp_by_daysupp(class=state agegroup agegroupnum zip_uncertain, outfile = _state_AG_UN, level=045);

			*Race/Hispanic Tables;
			%if "&race_out" = "Y" %then %do;
		 	 	%create_t5_cida_disp_by_daysupp(class=state race, outfile = _state_R, level=048);
				%create_t5_cida_disp_by_daysupp(class=state race zip_uncertain, outfile = _state_R_UN, level=049);
			%end;
			%if "&hispanic_out" = "Y" %then %do;
		 	 	 %create_t5_cida_disp_by_daysupp(class=state hispanic, outfile = _state_H, level=050);
				%create_t5_cida_disp_by_daysupp(class=state hispanic zip_uncertain, outfile = _state_H_UN, level=051);
			%end;
		%end;

		%if %index(&geog.,HHS_REG) > 0 %then %do;
			%create_t5_cida_disp_by_daysupp(class=hhs_reg, outfile = _hhs_reg, level=070);
			%create_t5_cida_disp_by_daysupp(class=hhs_reg zip_uncertain, outfile = _hhs_reg_UN, level=071);
			%create_t5_cida_disp_by_daysupp(class=hhs_reg sex, outfile = _hhs_reg_S, level=072);
			%create_t5_cida_disp_by_daysupp(class=hhs_reg sex zip_uncertain, outfile = _hhs_reg_S_UN, level=073);
			%create_t5_cida_disp_by_daysupp(class=hhs_reg agegroup agegroupnum , outfile = _hhs_reg_AG, level=074);
			%create_t5_cida_disp_by_daysupp(class=hhs_reg agegroup agegroupnum zip_uncertain, outfile = _hhs_reg_AG_UN, level=075);

			*Race/Hispanic Tables;
			%if "&race_out" = "Y" %then %do;
		 	 	%create_t5_cida_disp_by_daysupp(class=hhs_reg race, outfile = _hhs_reg_R, level=078);
				%create_t5_cida_disp_by_daysupp(class=hhs_reg race zip_uncertain, outfile = _hhs_reg_R_UN, level=079);
			%end;
			%if "&hispanic_out" = "Y" %then %do;
		 	 	 %create_t5_cida_disp_by_daysupp(class=hhs_reg hispanic, outfile = _hhs_reg_H, level=080);
				%create_t5_cida_disp_by_daysupp(class=hhs_reg hispanic zip_uncertain, outfile = _hhs_reg_H_UN, level=081);
			%end;
		%end;

		%if %index(&geog.,CB_REG) > 0 %then %do;
			%create_t5_cida_disp_by_daysupp(class=cb_reg, outfile = _cb_reg, level=090);
			%create_t5_cida_disp_by_daysupp(class=cb_reg zip_uncertain, outfile = _cb_reg_UN, level=091);
			%create_t5_cida_disp_by_daysupp(class=cb_reg sex, outfile = _cb_reg_S, level=092);
			%create_t5_cida_disp_by_daysupp(class=cb_reg sex zip_uncertain, outfile = _cb_reg_S_UN, level=093);
			%create_t5_cida_disp_by_daysupp(class=cb_reg agegroup agegroupnum , outfile = _cb_reg_AG, level=094);
			%create_t5_cida_disp_by_daysupp(class=cb_reg agegroup agegroupnum zip_uncertain, outfile = _cb_reg_AG_UN, level=095);

			*Race/Hispanic Tables;
			%if "&race_out" = "Y" %then %do;
		 	 	%create_t5_cida_disp_by_daysupp(class=cb_reg race, outfile = _cb_reg_R, level=098);
				%create_t5_cida_disp_by_daysupp(class=cb_reg race zip_uncertain, outfile = _cb_reg_R_UN, level=099);
			%end;
			%if "&hispanic_out" = "Y" %then %do;
		 	 	 %create_t5_cida_disp_by_daysupp(class=cb_reg hispanic, outfile = _cb_reg_H, level=100);
				%create_t5_cida_disp_by_daysupp(class=cb_reg hispanic zip_uncertain, outfile = _cb_reg_H_UN, level=101);
			%end;
		%end;
	%end;


	/****************************/
	/* t5_cida_from_studystart	*/
	/****************************/
	%macro create_t5_cida_from_studystart(data=, class=,outfile=,level=);
		proc means data=&data. nway noprint missing;
        	var Patient totrxsup EpisodeDispensing ;
        	class PatId &class.;
        	id patient;
        	output out=_num0(drop=_:) max(Patient)=Npts
                                  sum(Patient)=Episodes           
                                  sum(totrxsup)=Daysupp         
                                  sum(EpisodeDispensing)=Dispensings;             
        run;

		proc means data=_num0 nway noprint missing;
			var Npts Episodes Daysupp Dispensings;
		    class &class.;
		    id patient;
		    output out=&outfile.(drop=_:) sum=;                                  
		run;

		%if %length(&class.)=0 %then %do;
		    data _t5_cida&data.;
		    	set &outfile.(drop=patient);
		        format level $3. group $40.;
		        Level="&level.";
				group = "&ITGROUP.";
		    run;
		%end;
		%else %do;
			data _t5_cida&data.;
		    	set _t5_cida&data.
		        	&outfile.(drop=patient in=a);
		            if a then do;	
						Level="&level.";
						group = "&ITGROUP.";
					end;
			run;
		%end;

		proc datasets lib=work noprint nowarn; delete &outfile.; quit;
	%mend;

	%macro create_from_studystart_tables(data=);
		*Overall;
	    %create_t5_cida_from_studystart(data=&data., class=,outfile=_ALL,level=000);

		*Sex and Age Group;
		%create_t5_cida_from_studystart(data=&data., class=Sex,outfile=_S,Level=002);
	    %create_t5_cida_from_studystart(data=&data., class=AgeGroup AgeGroupNum,outfile=_AG,Level=003);
		%create_t5_cida_from_studystart(data=&data., class=Sex AgeGroup AgeGroupNum,outfile=_SAG,Level=004);

		*Months from Start;
		%create_t5_cida_from_studystart(data=&data., class=Mntsfromstart sex,outfile=_MS,Level=502);
		%create_t5_cida_from_studystart(data=&data., class=Mntsfromstart AgeGroup AgeGroupNum,outfile=_MAG,Level=503);
		%create_t5_cida_from_studystart(data=&data., class=Mntsfromstart sex AgeGroup AgeGroupNum,outfile=_MSAG,Level=504);
		%create_t5_cida_from_studystart(data=&data., class=Mntsfromstart,outfile=_M,Level=512);

		*Race Tables;
		%if "&race_out" = "Y" %then %do;
	   		%create_t5_cida_from_studystart(data=&data., class=race,outfile=_R,level=110);
	   		%create_t5_cida_from_studystart(data=&data., class=race sex,outfile=_RS,level=111);
	   		%create_t5_cida_from_studystart(data=&data., class=race agegroup agegroupnum,outfile=_RAG,level=112);
	   		%create_t5_cida_from_studystart(data=&data., class=race Mntsfromstart,outfile=_RM,level=505);
		%end;

		*Hispanic Tables;
		%if "&hispanic_out" = "Y" %then %do;
	   		%create_t5_cida_from_studystart(data=&data., class=hispanic,outfile=_H,level=115);
	   		%create_t5_cida_from_studystart(data=&data., class=hispanic sex,outfile=_HS,level=116);
	   		%create_t5_cida_from_studystart(data=&data., class=hispanic agegroup agegroupnum,outfile=_HAG,level=117);
	   		%create_t5_cida_from_studystart(data=&data., class=hispanic Mntsfromstart,outfile=_HM,level=506);
		%end;

		*Geographic Tables;
		%if %str(&GEOG.) ne %str() & %length(&GEOG.) > 0. %then %do;	
			%create_t5_cida_from_studystart(data=&data., class=zip_uncertain, outfile=_ZU, level=060);
			%create_t5_cida_from_studystart(data=&data., class=zip_uncertain sex, outfile=_ZU_S, level=061);
			%create_t5_cida_from_studystart(data=&data., class=zip_uncertain agegroup agegroupnum, outfile=_ZU_AG, level=062);

			%if %index(&geog.,ZIP3) > 0 %then %do;
				%create_t5_cida_from_studystart(data=&data., class=zip3, outfile=_zip3, level=020);
				%create_t5_cida_from_studystart(data=&data., class=zip3 zip_uncertain, outfile=_zip3_U, level=021);
				%create_t5_cida_from_studystart(data=&data., class=zip3 sex, outfile=_zip3_S, level=022);
				%create_t5_cida_from_studystart(data=&data., class=zip3 zip_uncertain sex, outfile=_zip3_S_U, level=023);
				%create_t5_cida_from_studystart(data=&data., class=zip3 agegroup agegroupnum, outfile=_zip3_AG, level=024);
				%create_t5_cida_from_studystart(data=&data., class=zip3 zip_uncertain agegroup agegroupnum, outfile=_zip3_AG_U, level=025);

				*Race/Hispanic Tables;
				%if "&race_out" = "Y" %then %do;
			 	 	%create_t5_cida_from_studystart(data=&data., class=zip3 race, outfile=_zip3_R, level=028);
					%create_t5_cida_from_studystart(data=&data., class=zip3 zip_uncertain race, outfile=_zip3_R_U, level=029);
				%end;
				%if "&hispanic_out" = "Y" %then %do;
			 	 	%create_t5_cida_from_studystart(data=&data., class=zip3 hispanic, outfile=_zip3_H, level=030);
					%create_t5_cida_from_studystart(data=&data., class=zip3 zip_uncertain hispanic, outfile=_zip3_H_U, level=031);
				%end;
			%end;

			%if %index(&geog.,STATE) > 0 %then %do;
				%create_t5_cida_from_studystart(data=&data., class=state, outfile=_state, level=040);
				%create_t5_cida_from_studystart(data=&data., class=state zip_uncertain, outfile=_state_U, level=041);
				%create_t5_cida_from_studystart(data=&data., class=state sex, outfile=_state_S, level=042);
				%create_t5_cida_from_studystart(data=&data., class=state zip_uncertain sex, outfile=_state_S_U, level=043);
				%create_t5_cida_from_studystart(data=&data., class=state agegroup agegroupnum, outfile=_state_AG, level=044);
				%create_t5_cida_from_studystart(data=&data., class=state zip_uncertain agegroup agegroupnum, outfile=_state_AG_U, level=045);

				*Race/Hispanic Tables;
				%if "&race_out" = "Y" %then %do;
			 	 	%create_t5_cida_from_studystart(data=&data., class=state race, outfile=_state_R, level=048);
					%create_t5_cida_from_studystart(data=&data., class=state zip_uncertain race, outfile=_state_R_U, level=049);
				%end;
				%if "&hispanic_out" = "Y" %then %do;
			 	 	%create_t5_cida_from_studystart(data=&data., class=state hispanic, outfile=_state_H, level=050);
					%create_t5_cida_from_studystart(data=&data., class=state zip_uncertain hispanic, outfile=_state_H_U, level=051);
				%end;
			%end;

			%if %index(&geog.,HHS_REG) > 0 %then %do;
				%create_t5_cida_from_studystart(data=&data., class=hhs_reg, outfile=_hhs_reg, level=070);
				%create_t5_cida_from_studystart(data=&data., class=hhs_reg zip_uncertain, outfile=_hhs_reg_U, level=071);
				%create_t5_cida_from_studystart(data=&data., class=hhs_reg sex, outfile=_hhs_reg_S, level=072);
				%create_t5_cida_from_studystart(data=&data., class=hhs_reg zip_uncertain sex, outfile=_hhs_reg_S_U, level=073);
				%create_t5_cida_from_studystart(data=&data., class=hhs_reg agegroup agegroupnum, outfile=_hhs_reg_AG, level=074);
				%create_t5_cida_from_studystart(data=&data., class=hhs_reg zip_uncertain agegroup agegroupnum, outfile=_hhs_reg_AG_U, level=075);

				*Race/Hispanic Tables;
				%if "&race_out" = "Y" %then %do;
			 	 	%create_t5_cida_from_studystart(data=&data., class=hhs_reg race, outfile=_hhs_reg_R, level=078);
					%create_t5_cida_from_studystart(data=&data., class=hhs_reg zip_uncertain race, outfile=_hhs_reg_R_U, level=079);
				%end;
				%if "&hispanic_out" = "Y" %then %do;
			 	 	%create_t5_cida_from_studystart(data=&data., class=hhs_reg hispanic, outfile=_hhs_reg_H, level=080);
					%create_t5_cida_from_studystart(data=&data., class=hhs_reg zip_uncertain hispanic, outfile=_hhs_reg_H_U, level=081);
				%end;
			%end;

			%if %index(&geog.,CB_REG) > 0 %then %do;
				%create_t5_cida_from_studystart(data=&data., class=cb_reg, outfile=_cb_reg, level=090);
				%create_t5_cida_from_studystart(data=&data., class=cb_reg zip_uncertain, outfile=_cb_reg_U, level=091);
				%create_t5_cida_from_studystart(data=&data., class=cb_reg sex, outfile=_cb_reg_S, level=092);
				%create_t5_cida_from_studystart(data=&data., class=cb_reg zip_uncertain sex, outfile=_cb_reg_S_U, level=093);
				%create_t5_cida_from_studystart(data=&data., class=cb_reg agegroup agegroupnum, outfile=_cb_reg_AG, level=094);
				%create_t5_cida_from_studystart(data=&data., class=cb_reg zip_uncertain agegroup agegroupnum, outfile=_cb_reg_AG_U, level=095);

				*Race/Hispanic Tables;
				%if "&race_out" = "Y" %then %do;
			 	 	%create_t5_cida_from_studystart(data=&data., class=cb_reg race, outfile=_cb_reg_R, level=098);
					%create_t5_cida_from_studystart(data=&data., class=cb_reg zip_uncertain race, outfile=_cb_reg_R_U, level=099);
				%end;
				%if "&hispanic_out" = "Y" %then %do;
			 	 	%create_t5_cida_from_studystart(data=&data., class=cb_reg hispanic, outfile=_cb_reg_H, level=100);
					%create_t5_cida_from_studystart(data=&data., class=cb_reg zip_uncertain hispanic, outfile=_cb_reg_H_U, level=101);
				%end;
			%end;
		%end;
	%mend;

	****FirstEps****;
		data _firsteps;
			set _alleps;
			where episodenum = 1 | patient = 0;
		run;
	%create_from_studystart_tables(data=_firsteps);

	****AllEps****;
	%create_from_studystart_tables(data=_alleps);


	/****************************/
	/* t5_cida_episode_duration	*/
	/****************************/
  	%macro t5_cida_episode_duration_gaps(class=,outfile=,level=, dataout=);
        proc means data=_alleps nway noprint missing;
	        var patient;
	        class patid &class.;
	        id patient;
	        output out=_num0(drop=_:) sum(patient)=Episodes max(patient) = npts;                                  
        run;

		proc means data=_num0 nway noprint missing;
			var Npts Episodes;
		    class &class.;
		    id patient;
		    output out=&outfile.(drop=_:) sum=;                                  
		run;

	    %if %length(&class.)=0 %then %do;
	    	data &dataout.;
	            set &outfile.(drop=patient);
	            format level $3. group $40.;
	            Level="&level.";
				group = "&ITGROUP.";
	    	run;
		%end;
	    %else %do;
	    	data &dataout.;
	        	set &dataout.
	                &outfile.(drop=patient in=a);
	                if a then do;	
						Level="&level.";
						group = "&ITGROUP.";
					end;
			run;
		%end;

		proc datasets lib=work noprint nowarn; delete &outfile.; quit;
	%mend;

	*Overall;
    %t5_cida_episode_duration_gaps(class=,outfile=_ALL,level=000, dataout=_t5_cida_episdur);

	*Sex and Age Group;
	%t5_cida_episode_duration_gaps(class=Sex,outfile=_S,Level=002, dataout=_t5_cida_episdur);
    %t5_cida_episode_duration_gaps(class=AgeGroup AgeGroupNum,outfile=_AG,Level=003, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=Sex AgeGroup AgeGroupNum,outfile=_SAG,Level=004, dataout=_t5_cida_episdur);

	*CumEpisodeLength;
	%t5_cida_episode_duration_gaps(class=CumEpisodeLength,outfile=_EL,Level=520, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=CumEpisodeLength sex,outfile=_ELS,Level=522, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=CumEpisodeLength agegroup AgeGroupNum,outfile=_ELAG,Level=523, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=CumEpisodeLength sex agegroup AgeGroupNum,outfile=_ELSAG,Level=524, dataout=_t5_cida_episdur);

	*EpisodeNum;
	%t5_cida_episode_duration_gaps(class=EpisodeNum,outfile=_EN,Level=530, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=EpisodeNum sex,outfile=_ENS,Level=532, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=EpisodeNum agegroup AgeGroupNum,outfile=_ENAG,Level=533, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=EpisodeNum sex agegroup AgeGroupNum,outfile=_ENSAG,Level=534, dataout=_t5_cida_episdur);

	*EpisodeLength*EpisodeNum;
	%t5_cida_episode_duration_gaps(class=EpisodeNum EpisodeLength,outfile=_EN_EL,Level=540, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=EpisodeNum EpisodeLength sex,outfile=_EN_EL_S,Level=542, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=EpisodeNum EpisodeLength agegroup agegroupnum,outfile=_EN_EL_AG,Level=543, dataout=_t5_cida_episdur);
	%t5_cida_episode_duration_gaps(class=EpisodeNum EpisodeLength sex agegroup agegroupnum,outfile=_EN_EL_SAG,Level=544, dataout=_t5_cida_episdur);

	*Race Tables;
	%if "&race_out" = "Y" %then %do;
		%t5_cida_episode_duration_gaps(class=race,outfile=_R,Level=110, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=race sex,outfile=_RS,Level=111, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=race agegroup agegroupnum ,outfile=_RAG,Level=112, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=race CumEpisodeLength,outfile=_REL,Level=525, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=race EpisodeNum,outfile=_REN,Level=535, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=race EpisodeNum EpisodeLength,outfile=_REN_EL,Level=545, dataout=_t5_cida_episdur);
	%end;

	*Hispanic Tables;
	%if "&hispanic_out" = "Y" %then %do;
		%t5_cida_episode_duration_gaps(class=hispanic,outfile=_H,Level=115, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=hispanic sex,outfile=_HS,Level=116, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=hispanic agegroup agegroupnum ,outfile=_HAG,Level=117, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=hispanic CumEpisodeLength,outfile=_HEL,Level=526, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=hispanic EpisodeNum,outfile=_HEN,Level=536, dataout=_t5_cida_episdur);
		%t5_cida_episode_duration_gaps(class=hispanic EpisodeNum EpisodeLength,outfile=_HEN_EL,Level=546, dataout=_t5_cida_episdur);
	%end;

	%macro epis_dur_gap_geo(dataout=);
		%t5_cida_episode_duration_gaps(class=zip_uncertain,outfile=_ZU,level=060, dataout=&dataout.);
		%t5_cida_episode_duration_gaps(class=zip_uncertain sex,outfile=_ZU_S,level=061, dataout=&dataout.);
		%t5_cida_episode_duration_gaps(class=zip_uncertain agegroupnum agegroup,outfile=_ZU_AG,level=062, dataout=&dataout.);

		%if %index(&geog.,ZIP3) > 0 %then %do;
			%t5_cida_episode_duration_gaps(class=zip3,outfile=_zip3,level=020, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=zip3 zip_uncertain ,outfile=_zip3_UN,level=021, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=zip3 sex,outfile=_zip3_S,level=022, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=zip3 sex zip_uncertain,outfile=_zip3_S_UN,level=023, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=zip3 agegroup agegroupnum,outfile=_zip3_AG,level=024, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=zip3 agegroup agegroupnum zip_uncertain,outfile=_zip3_AG_UN,level=025, dataout=&dataout.);

			*Race/Hispanic Tables;
			%if "&race_out" = "Y" %then %do;
			 	%t5_cida_episode_duration_gaps(class=zip3 race,outfile=_zip3_R,level=028, dataout=&dataout.);
				%t5_cida_episode_duration_gaps(class=zip3 race zip_uncertain,outfile=_zip3_R_UN,level=029, dataout=&dataout.);
			%end;
			%if "&hispanic_out" = "Y" %then %do;
			 	%t5_cida_episode_duration_gaps(class=zip3 hispanic,outfile=_zip3_H,level=030, dataout=&dataout.);
				%t5_cida_episode_duration_gaps(class=zip3 hispanic zip_uncertain,outfile=_zip3_H_UN,level=031, dataout=&dataout.);
			%end;
		%end;

		%if %index(&geog.,STATE) > 0 %then %do;
			%t5_cida_episode_duration_gaps(class=state,outfile=_state,level=040, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=state zip_uncertain ,outfile=_state_UN,level=041, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=state sex,outfile=_state_S,level=042, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=state sex zip_uncertain,outfile=_state_S_UN,level=043, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=state agegroup agegroupnum,outfile=_state_AG,level=044, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=state agegroup agegroupnum zip_uncertain,outfile=_state_AG_UN,level=045, dataout=&dataout.);

			*Race/Hispanic Tables;
			%if "&race_out" = "Y" %then %do;
			 	%t5_cida_episode_duration_gaps(class=state race,outfile=_state_R,level=048, dataout=&dataout.);
				%t5_cida_episode_duration_gaps(class=state race zip_uncertain,outfile=_state_R_UN,level=049, dataout=&dataout.);
			%end;
			%if "&hispanic_out" = "Y" %then %do;
			 	%t5_cida_episode_duration_gaps(class=state hispanic,outfile=_state_H,level=050, dataout=&dataout.);
				%t5_cida_episode_duration_gaps(class=state hispanic zip_uncertain,outfile=_state_H_UN,level=051, dataout=&dataout.);
			%end;
		%end;

		%if %index(&geog.,HHS_REG) > 0 %then %do;
			%t5_cida_episode_duration_gaps(class=hhs_reg,outfile=_hhs_reg,level=070, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=hhs_reg zip_uncertain ,outfile=_hhs_reg_UN,level=071, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=hhs_reg sex,outfile=_hhs_reg_S,level=072, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=hhs_reg sex zip_uncertain,outfile=_hhs_reg_S_UN,level=073, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=hhs_reg agegroup agegroupnum,outfile=_hhs_reg_AG,level=074, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=hhs_reg agegroup agegroupnum zip_uncertain,outfile=_hhs_reg_AG_UN,level=075, dataout=&dataout.);

			*Race/Hispanic Tables;
			%if "&race_out" = "Y" %then %do;
			 	%t5_cida_episode_duration_gaps(class=hhs_reg race,outfile=_hhs_reg_R,level=078, dataout=&dataout.);
				%t5_cida_episode_duration_gaps(class=hhs_reg race zip_uncertain,outfile=_hhs_reg_R_UN,level=079, dataout=&dataout.);
			%end;
			%if "&hispanic_out" = "Y" %then %do;
			 	%t5_cida_episode_duration_gaps(class=hhs_reg hispanic,outfile=_hhs_reg_H,level=080, dataout=&dataout.);
				%t5_cida_episode_duration_gaps(class=hhs_reg hispanic zip_uncertain,outfile=_hhs_reg_H_UN,level=081, dataout=&dataout.);
			%end;
		%end;

		%if %index(&geog.,CB_REG) > 0 %then %do;
			%t5_cida_episode_duration_gaps(class=cb_reg,outfile=_cb_reg,level=090, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=cb_reg zip_uncertain ,outfile=_cb_reg_UN,level=091, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=cb_reg sex,outfile=_cb_reg_S,level=092, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=cb_reg sex zip_uncertain,outfile=_cb_reg_S_UN,level=093, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=cb_reg agegroup agegroupnum,outfile=_cb_reg_AG,level=094, dataout=&dataout.);
			%t5_cida_episode_duration_gaps(class=cb_reg agegroup agegroupnum zip_uncertain,outfile=_cb_reg_AG_UN,level=095, dataout=&dataout.);

			*Race/Hispanic Tables;
			%if "&race_out" = "Y" %then %do;
			 	%t5_cida_episode_duration_gaps(class=cb_reg race,outfile=_cb_reg_R,level=098, dataout=&dataout.);
				%t5_cida_episode_duration_gaps(class=cb_reg race zip_uncertain,outfile=_cb_reg_R_UN,level=099, dataout=&dataout.);
			%end;
			%if "&hispanic_out" = "Y" %then %do;
			 	%t5_cida_episode_duration_gaps(class=cb_reg hispanic,outfile=_cb_reg_H,level=100, dataout=&dataout.);
				%t5_cida_episode_duration_gaps(class=cb_reg hispanic zip_uncertain,outfile=_cb_reg_H_UN,level=101, dataout=&dataout.);
			%end;
		%end;
	%mend;

	%if %str(&GEOG.) ne %str() & %length(&GEOG.) > 0. %then %do;	
		%epis_dur_gap_geo(dataout=_t5_cida_episdur);
	%end;


	/****************/
	/* t5_cida_gaps	*/
	/****************/

	*Overall;
    %t5_cida_episode_duration_gaps(class=,outfile=_ALL,level=000, dataout=_t5_cida_gaps);

	*Sex and Age Group;
	%t5_cida_episode_duration_gaps(class=Sex,outfile=_S,Level=002, dataout=_t5_cida_gaps);
    %t5_cida_episode_duration_gaps(class=AgeGroup AgeGroupNum,outfile=_AG,Level=003, dataout=_t5_cida_gaps);
	%t5_cida_episode_duration_gaps(class=Sex AgeGroup AgeGroupNum,outfile=_SAG,Level=004, dataout=_t5_cida_gaps);

	*GapLength;
	%t5_cida_episode_duration_gaps(class=GapLength,outfile=_GL,Level=550, dataout=_t5_cida_gaps);
	%t5_cida_episode_duration_gaps(class=GapLength sex,outfile=_GLS,Level=552, dataout=_t5_cida_gaps);
	%t5_cida_episode_duration_gaps(class=GapLength agegroup agegroupnum,outfile=_GLAG,Level=553, dataout=_t5_cida_gaps);
	%t5_cida_episode_duration_gaps(class=GapLength agegroup agegroupnum sex,outfile=_GLAGS,Level=554, dataout=_t5_cida_gaps);

	*GapNum;
	%t5_cida_episode_duration_gaps(class=GapNum,outfile=_GN,Level=560, dataout=_t5_cida_gaps);
	%t5_cida_episode_duration_gaps(class=GapNum sex,outfile=_GNS,Level=562, dataout=_t5_cida_gaps);	
	%t5_cida_episode_duration_gaps(class=GapNum agegroup agegroupnum,outfile=_GNAG,Level=563, dataout=_t5_cida_gaps);	
	%t5_cida_episode_duration_gaps(class=GapNum sex agegroup agegroupnum,outfile=_GNSAG,Level=564, dataout=_t5_cida_gaps);	

	*GapNum*GapLength;
	%t5_cida_episode_duration_gaps(class=GapNum GapLength,outfile=_GN_GL,Level=570, dataout=_t5_cida_gaps);
	%t5_cida_episode_duration_gaps(class=GapNum GapLength sex,outfile=_GN_GL_S,Level=572, dataout=_t5_cida_gaps);
	%t5_cida_episode_duration_gaps(class=GapNum GapLength agegroup agegroupnum,outfile=_GN_GL_AG,Level=573, dataout=_t5_cida_gaps);
	%t5_cida_episode_duration_gaps(class=GapNum GapLength agegroup agegroupnum sex,outfile=_GN_GL_SAG,Level=574, dataout=_t5_cida_gaps);

	*Race Tables;
	%if "&race_out" = "Y" %then %do;
		%t5_cida_episode_duration_gaps(class=race,outfile=_R,Level=110, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=race Sex,outfile=_RS,Level=111, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=race agegroup agegroupnum,outfile=_RAG,Level=112, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=race GapLength,outfile=_R_GL,Level=555, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=race GapNum,outfile=_R_GN,Level=565, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=race GapNum GapLength,outfile=_R_GL_GN,Level=575, dataout=_t5_cida_gaps);
	%end;

	*Hispanic Tables;
	%if "&hispanic_out" = "Y" %then %do;
		%t5_cida_episode_duration_gaps(class=hispanic,outfile=_H,Level=115, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=hispanic Sex,outfile=_HS,Level=116, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=hispanic agegroup agegroupnum,outfile=_HAG,Level=117, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=hispanic GapLength,outfile=_H_GL,Level=556, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=hispanic GapNum,outfile=_H_GN,Level=566, dataout=_t5_cida_gaps);
		%t5_cida_episode_duration_gaps(class=hispanic GapNum GapLength,outfile=_H_GL_GN,Level=576, dataout=_t5_cida_gaps);
	%end;

	%if %str(&GEOG.) ne %str() & %length(&GEOG.) > 0. %then %do;	
		%epis_dur_gap_geo(dataout=_t5_cida_gaps);
	%end;

	/****************************/
	/* t5_cida_episdur_censor	*/
	/****************************/
	%macro t5_cida_episdur_censor(class=,outfile=,level=);
        proc means data=_alleps nway noprint missing;
	        var patient;
	        class patid &class.;
	        id patient;
	        output out=_num0(drop=_:) 	sum(patient)=Episodes
										max(patient)=Npts
										sum(episode_cens_elig)=cens_elig
										sum(episode_cens_dth)=cens_dth
										sum(episode_cens_dpend)=cens_dpend
										sum(episode_cens_qryend)=cens_qryend
										sum(episode_cens_episend)=cens_episend
										sum(episode_cens_spec)=cens_spec;
        run;

		proc means data=_num0 nway noprint missing;
			var Npts Episodes cens_elig cens_dth cens_dpend cens_qryend cens_episend cens_spec;
		    class &class.;
		    id patient;
		    output out=&outfile.(drop=_:) sum=;                                  
		run;


	    %if %length(&class.)=0 %then %do;
	    	data _t5_cida_episdur_censor;
	            set &outfile.(drop=patient);
	            format level $3. group $40.;
	            Level="&level.";
				group = "&ITGROUP.";
	    	run;
		%end;
	    %else %do;
	    	data _t5_cida_episdur_censor;
	        	set _t5_cida_episdur_censor
	                &outfile.(drop=patient in=a);
	                if a then do;	
						Level="&level.";
						group = "&ITGROUP.";
					end;
			run;
		%end;

		proc datasets lib=work noprint nowarn; delete &outfile.; quit;
	%mend;

	*Overall;
    %t5_cida_episdur_censor(class=,outfile=_ALL,level=000);

	*EpisodeLength / EpisodeNum;
	%t5_cida_episdur_censor(class=CumEpisodeLength,outfile=_EL,Level=520);
	%t5_cida_episdur_censor(class=EpisodeNum,outfile=_EN,Level=530);
	%t5_cida_episdur_censor(class=EpisodeNum EpisodeLength,outfile=_EN_EL,Level=540);


	/*** Initalize race/hispanic and geographical regions if missing ***/
	%macro init_race_hispanic_geog(data=);
		data &data.;
			length race hispanic zip_uncertain $1. zip3 state hhs_reg cb_reg $7.;
			set &data.;
			%if "&race_out" = "N" %then %do;
				race = '';
			%end;
			%if "&hispanic_out" = "N" %then %do;
				hispanic = '';
			%end;
			%if %index(&geog, ZIP3) = 0 %then %do;
				zip3 = '';
			%end;
			%if %index(&geog, STATE) = 0 %then %do;
				state = '';
			%end;
			%if %index(&geog, HHS_REG) = 0 %then %do;
				hhs_reg = '';
			%end;
			%if %index(&geog, CB_REG) = 0 %then %do;
				cb_reg = '';
			%end;
			%if %str(&geog.) = %str() or %length(&geog.) = 0. %then %do;	
				zip_uncertain = '';
			%end;
		run;
	%mend;
	%init_race_hispanic_geog(data=_t5_cida_disp_by_daysupp);
	%init_race_hispanic_geog(data=_t5_cida_firsteps);
	%init_race_hispanic_geog(data=_t5_cida_alleps);
	%init_race_hispanic_geog(data=_t5_cida_episdur);
	%init_race_hispanic_geog(data=_t5_cida_gaps);


	proc sort data=_t5_cida_disp_by_daysupp;
		by level sex agegroupnum zip3 state hhs_reg cb_reg zip_uncertain race hispanic;
	run;
	proc sort data=_t5_cida_firsteps;
		by level sex agegroupnum mntsfromstart zip3 state hhs_reg cb_reg zip_uncertain race hispanic;
	run;
	proc sort data=_t5_cida_alleps;
		by level sex agegroupnum mntsfromstart zip3 state hhs_reg cb_reg zip_uncertain race hispanic;
	run;
	proc sort data=_t5_cida_episdur;
		by level sex agegroupnum cumepisodelength episodenum episodelength zip3 state hhs_reg cb_reg zip_uncertain race hispanic;
	run;
	proc sort data=_t5_cida_gaps;
		by level sex agegroupnum gaplength gapnum zip3 state hhs_reg cb_reg zip_uncertain race hispanic;
	run;
	proc sort data=_t5_cida_episdur_censor;
		by level cumepisodelength episodenum episodelength ;
	run;

	/*** Output to MSOC ***/
	%if %eval(&group.=1) %then %do;

		data msoc.&runid._t5_cida_disp_by_daysupp;
			retain group level sex agegroup agegroupnum zip3 state hhs_reg cb_reg zip_uncertain race hispanic daysupp dispensings;
			set _t5_cida_disp_by_daysupp;
		run;

		data msoc.&runid._t5_cida_firsteps;
			retain group level sex agegroup agegroupnum mntsfromstart zip3 state hhs_reg cb_reg zip_uncertain race hispanic episodes npts daysupp dispensings;
			set _t5_cida_firsteps;
		run;

		data msoc.&runid._t5_cida_alleps;
			retain group level sex agegroup agegroupnum mntsfromstart zip3 state hhs_reg cb_reg zip_uncertain race hispanic episodes npts daysupp dispensings;
			set _t5_cida_alleps;
		run;

		data msoc.&runid._t5_cida_episdur;
			retain group level sex agegroup agegroupnum cumepisodelength episodenum episodelength zip3 state hhs_reg cb_reg zip_uncertain race hispanic episodes npts;
			set _t5_cida_episdur;
		run;

		data msoc.&runid._t5_cida_gaps;
			retain group level sex agegroup agegroupnum gaplength gapnum race hispanic zip3 state hhs_reg cb_reg zip_uncertain episodes;
			set _t5_cida_gaps;
		run;

		data msoc.&runid._t5_cida_episdur_censor;
			retain group level cumepisodelength episodenum episodelength episodes npts;
			set _t5_cida_episdur_censor;
		run;

	%end;
	%else %do;
 		proc append base=msoc.&runid._t5_cida_disp_by_daysupp
	                    data=_t5_cida_disp_by_daysupp force;
	    run;

 		proc append base=msoc.&runid._t5_cida_firsteps
	                    data=_t5_cida_firsteps force;
	    run;

 		proc append base=msoc.&runid._t5_cida_alleps
	                    data=_t5_cida_alleps force;
	    run;

 		proc append base=msoc.&runid._t5_cida_episdur
	                    data=_t5_cida_episdur force;
	    run;

 		proc append base=msoc.&runid._t5_cida_gaps
	                    data=_t5_cida_gaps force;
	    run;

 		proc append base=msoc.&runid._t5_cida_episdur_censor
	                    data=_t5_cida_episdur_censor force;
	    run;

	%end;

%put NOTE: ******** END OF MACRO: ms_cidatablest5  v1.3 ********;

%mend ms_cidatablest5;