****************************************************************************************************
****************************************************************************************************
* PROGRAM: codedistribution_createdata.sas
* Created (mm/dd/yyyy): 04/16/2021
*--------------------------------------------------------------------------------------------------
* PURPOSE: The macro creates aggregated code distribution dataset and process code descriptions
*--------------------------------------------------------------------------------------------------
* Sentinel Coordinating Center
* info@sentinelsystem.org
***************************************************************************************************;
%macro codedistribution_createdata;
select count(*) into :numgroupscodedist trimmed
%do loopcount = 1 %to &numgroupscodedist.;
/* set parameters for this loop */
call symputx('runid', runid);
call symputx('group', group);
call symputx('order', order);
if index(upcase(codedist), "EXP") > 0 then call symputx('codedistexp', 'Y');
if index(upcase(codedist), "HOI") > 0 then call symputx('codedisthoi', 'Y');
/* Aggregate results and keep only relevant distindextype */
proc means data=agg_distindex (where=(lowcase(group)="&group." and runid="&runid.")) noprint nway missing;
class group runid distindextype distindexlist dpidsiteid;
output out=_agg_distindex(drop=_:) sum=;
%if &codedistexp. ne Y %then %do;
if lowcase(distindextype)="exp" then delete;
%if &codedisthoi. ne Y %then %do;
if lowcase(distindextype)="hoi" then delete;
proc append data=_agg_distindex base=_distindex force; run;
%end; /*loop through code distribution groups*/
data _distindex (drop = nwords count);
nwords = countw(distindexlist, '_', 'oq');
ixid = scan(distindexlist, count, '_', 'oq');
if count > 1 then episodes = 0;
/* Create one table for the unique mapping values. If RX, use stockgroup as code */
data _mappingfile (drop = stockgroup _code);
set agg_distindexmap (rename = (code = _code));
if not missing(stockgroup) then do;