30%macro irmc_apply_rollup_aggregation( aggregation_rules =
40 %
if not %symexist(log_level) %then %let log_level = 0;
41 %
if &log_level. in (3 4 5) %then %
do;
47 %
if(not %rsk_dsexist(&aggregation_rules.)) %then %
do;
49 %put %str(ERR)OR: aggregation rules table &aggregation_rules. does not exist or
this user does not have access to it. No aggregations can be performed.;
54 data applicable_aggregations;
55 set &aggregation_rules.;
56 where lowcase(TGT_TABLE_SCHEMA_NM) = lowcase(
"&schema_name.")
57 and lowcase(resolve(TGT_TABLE_SCHEMA_VER)) = lowcase("&schema_version.")
58 and TGT_TABLE_SCHEMA_NM = SRC_TABLE_SCHEMA_NM
59 and TGT_TABLE_SCHEMA_VER = SRC_TABLE_SCHEMA_VER
61 %if &debug. eq Y %then %do;
63 put '--- Aggregation to be applied:';
65 put TGT_TABLE_SCHEMA_NM=;
66 put TGT_TABLE_SCHEMA_VER=;
74 put '------------------ ';
80 %let TotRows = %rsk_attrn(work.applicable_aggregations, nobs);
81 %if &TotRows. eq 0 %then %do;
83 %put %str(WAR)NING: No aggregation rules in &aggregation_rules. apply to table with schema_name &schema_name. and schema_version &schema_version.. No aggregations will be performed.;
97 : TGT_ID_COLS separated by ' ',
98 : TGT_VAL_COLS separated by ' ',
99 : SRC_COLS separated by ' '
101 work.applicable_aggregations
104 %let success_flg = N;
105 %let missing_inst_var = ;
106 %rsk_verify_ds_col(REQUIRED_COL_LIST = &period_column. &TGT_ID_COLS. &TGT_VAL_COLS. &SRC_COLS.,
107 IN_DS_LIB =%scan(&input_ds,1),
108 IN_DS_NM =%scan(&input_ds,2),
109 OUT_SUCCESS_FLG =success_flg,
110 OUT_MISSING_VAR =missing_inst_var);
112 %if %upcase(&success_flg) eq N %then %do;
114 %put %str(ERR)OR: Required variable "&missing_inst_var" is missing in data set "&input_ds". No aggregations will be performed;
119 data aggregations_parsed;
120 set applicable_aggregations end = last;
121 length parsedRule $ 12000 tempstr $12000 INIT_STRING $32000 GETVAL_STR $32000 APPLYAGG_STR $32000 AGGCRITERIA_STR $32000;
122 index = find(SRC_WHERE_COND,'%GETVAL(');
126 index2 = findc(SRC_WHERE_COND,')',index);
128 parsedRule = strip(parsedRule) || substr(SRC_WHERE_COND,index,index2-index) || ',__x' || strip(count) || ');';
130 SRC_WHERE_COND = substr(SRC_WHERE_COND,1,index-1) || '__x' || strip(count) || substr(SRC_WHERE_COND,index2+1);
132 SRC_WHERE_COND = '__x' || strip(count) || substr(SRC_WHERE_COND,index2+1);
134 index = find(SRC_WHERE_COND,'%GETVAL(');
136 parsedRule = strip(parsedRule) || strip(TGT_TBL_VAL_COL) || ' = coalesce(' || strip(SRC_WHERE_COND) || ',' || strip(TGT_TBL_VAL_COL) || ');';
137 parsedRule =tranwrd(parsedRule, "GETVAL", "GETVAL"||strip(_N_));
141'%macro INITHASH'||strip(_N_)||'();
142if missing(_temp_hashinit'||strip(_N_)||') then do;
143 declare hash hash'||strip(_N_)||'(dataset: "' || "&input_ds." ||'");
144 _temp_rc = hash'||strip(_N_)||'.defineKey("' || strip(SRC_COL) || '","' || "&period_column." || '");
145 _temp_rc = hash'||strip(_N_)||'.defineData("' || strip(TGT_TBL_VAL_COL) || '");
146 _temp_rc = hash'||strip(_N_)||'.defineDone();
147 _temp_hashinit'||strip(_N_)||' = 1;
150 call execute(INIT_STRING);
156'%macro GETVAL'||strip(_N_)||'(col_val,period,varname);
157 _temp_col_id = '|| strip(SRC_COL) || '; ' || SRC_COL || ' = strip("&col_val.");
158 _temp_period = ' || "&period_column.; &period_column. = " || '_temp_period - &period.;
159 _temp_col_val =' || strip(TGT_TBL_VAL_COL) ||';
160 call missing(' || strip(TGT_TBL_VAL_COL) || ');
161 _temp_rc = hash'||strip(_N_)||'.find();
162 if _temp_rc ne 0 then do;
163 put "Lookup failed: col_val=&col_val";
164 put &period_column.=;
165 put ' || strip(SRC_COL) || '=;
168 &varname. = ' || strip(TGT_TBL_VAL_COL) || '; ' ||
169 TGT_TBL_VAL_COL || ' = _temp_col_val; ' ||
170 SRC_COL || ' = _temp_col_id; ' || "
171 &period_column. = _temp_period; " || '
173 call execute(GETVAL_STR);
177'%macro APPLYAGG'||strip(_N_)||'(); ' ||
180 call execute(APPLYAGG_STR);
184'%macro AGGCRITERIA'||strip(_N_)||'();
185 upcase(' || strip(TGT_TBL_ID_COL) || ') eq upcase("' || strip(TGT_TBL_ID_VAL) || '")
187 call execute(AGGCRITERIA_STR);
189 %if &debug. eq Y %then %do;
191 put '--- Parsed aggregation is:';
197 %printMacro(INIT_STRING);
198 %printMacro(GETVAL_STR);
199 %printMacro(APPLYAGG_STR);
200 %printMacro(AGGCRITERIA_STR);
202 put '------------------ ';
207 call symputX('N_AGGS',_N_);
218 length _temp_col_id $ 512;
219 %do i=1 %to &N_AGGS.;
220 if %AGGCRITERIA&i.() then do;
228 %if &debug. eq N %then %do;
234%mend irmc_apply_rollup_aggregation;
237%macro printMacro(varname);
239 ind = findc(&varname.,';');
241 do while(ind>0 and cnt<1000);
242 tempstr = substr(&varname.,pind,ind-pind+1);
245 nind = findc(substr(&varname.,ind+1),';');
246 ind = min(1,nind)*(nind + ind);