************************************************************************************************
*************************************************************************************************
* PROGRAM: aggregate_tree.sas
* Created (mm/dd/yy): 04/28/2021
*--------------------------------------------------------------------------------------------------
* PURPOSE: Create analytic datasets that can be used as inputs to TreeScan software
* msoc.[RUNID]_t#_treeads_[TREEANALYSISID]_[LEVELID]_[LEVELNUM]_[PERIODID].sas7bdat
* msoc.[RUNID]_t3_tree_wkdays_[PERIODID]_agg.sas7bdat
* msoc.[RUNID]_t#_treeads_[TREEANALYSISID]_[LEVELID]_[LEVELNUM]_[PERIODID].sas7bdat
*------------------------------------------------------------------------------------------------
* Sentinel Coordinating Center
* info@sentinelsystem.org
***************************************************************************************************;
%do n = 1 %to &numrunid.;
%let runid = %scan(&runidlist., &n.);
%if %index(&reporttype., TREE) > 0 %then %do;
%let type = %substr(&reporttype,5,1);
%let type = %substr(&reporttype.,2,1);
/***********************************************************************************************
Identify EOI and REF for type 2 and 4
***********************************************************************************************/
*Get EOI/REF for type 2/4 analysis;
%if %eval(&type. ne 3) %then %do;
create table comparison as
select x.analysisgrp, y.eoi, y.ref
from infolder.&&&runid._psmatchfile. (keep=analysisgrp psestimategrp) as x
inner join infolder.&&&runid._psestimationfile. as y
on x.psestimategrp = y.psestimategrp;
/***********************************************************************************************
Determine if there is a t3treewkdays dataset created
***********************************************************************************************/
%if %sysfunc(exist(agg_t3_tree_wkdays_&periodid)) =1 %then %do;
select case when count(levelid) > 0 then 'Y'
else 'N' end into: run_t3wk
from infolder.&&&runid._userstrata. (where = (lowcase(tableid) = 't3treewkdays'));
/***********************************************************************************************
Determine distinct list of levelvars to use in aggregation based on userstrata file
***********************************************************************************************/
%macro find_lvl_vars (strata_table = , out_var =);
%global &out_var. num_&out_var.;
select distinct(b.levelvars) into: all_levelvars separated by ' '
from %if "&strata_table." = "t3treewkdays" %then %do;
infolder.&&&runid._userstrata. (where = (lowcase(tableid) = "&strata_table.")) b;
input.&treeaggfile.(where = (lowcase(runid)= "&runid.")) a
inner join infolder.&&&runid._userstrata. (where = (lowcase(tableid) = "&strata_table.")) b
on a.levelid = b.levelid;
%let num_&out_var.= %sysfunc(countw(&all_levelvars,' '));
%if &&num_&out_var.. > 0 %then %do;
%do u = 1 %to &&num_&out_var..;
levelvar = scan("&all_levelvars.",&u.); output;