****************************************************************************************************
****************************************************************************************************
* PROGRAM: baseline_aggregate.sas
* Created (mm/dd/yyyy): 10/20/2020
*--------------------------------------------------------------------------------------------------
* PURPOSE: The macro imports and concatenates both Level 1 and Level 2 baseline tables
* - Level 1 tables in the format: [runid]_baseline_[cohort]_[periodid].sas7bdat
* - Level 2 tables in the format: [runid]_adjusted_baseline_[periodid].sas7bdat
* - Dataset where each row represents a baseline metric and each column represents a DP value.
* Groups in the GROUPTABLE are stacked
* - dpsiteid: name of DP to import. Will be used as libname
- dpnumber: number to assign to each DP after transpose
* - grouptable: name of the dataset that contains groups, runIDs, and infomation pertaining to the
* baseline table name to include in the report.
* - outdata: name of the aggregated dataset
* - periodid: monitoring period #
* - This macro is designed to be called within a loop that loops through each data partner
* - For level 1 requests, the baseline table file name will be dynamically derived using
* &dpsiteid as the libname and the file name as: [runid]_baseline_[cohort]_&periodid
* - GROUPTABLE must contain the following variables: GROUP, RUNID, COHORT, and MERGEVAR
* For level 2 requests, only GROUP and RUNID need to be populated. GROUP will represent the ANALYSISGRP.
* For level 1 requests, COHORT is optional and will be used to correctly name the associated baseline
* table ([runid]_baseline_[cohort]_[periodid].sas7bdat), MERGEVAR is required and is the name of
* the key variable in the baseline table (group or analysisgrp)
* - It is assumed the user intends to import and aggregate all groups in the GROUPTABLE input file.
* The user should restrict this file to only groups with a baseline table prior to executing
*--------------------------------------------------------------------------------------------------
* Sentinel Coordinating Center
* info@sentinelsystem.org
***************************************************************************************************;
%macro baseline_aggregate(dpsiteid = ,
%put =====> MACRO CALLED: baseline_aggregate;
/*********************************************************************************************/
/* Level 1 baseline tables
/*********************************************************************************************/
%if %eval(&level.=1) %then %do;
/*Use GROUPTABLE to create list of baseline tables and create dataset in statement*/
data _temp_baseline_tablenames;
format baselinetablename $40.;
if missing(cohort) then do;
baselinetablename = lowcase(cats("&dpsiteid..",runid, "_baseline_&periodid"));
baselinetablename = lowcase(cats("&dpsiteid..",runid, "_baseline_", cohort, "_&periodid"));
select distinct baselinetablename into: baselinetables separated by ' '
from _temp_baseline_tablenames;
select count(distinct baselinetablename) into: num_unique_baseline_tables
from _temp_baseline_tablenames;
%put Extracting baseline tables: &baselinetables.;
/*Loop through each baseline table, import, stack groups*/
%do b = 1 %to %eval(&num_unique_baseline_tables.);
%let infile = %scan(&baselinetables., &b., ' ');