1
0
mirror of https://github.com/sasjs/core.git synced 2025-12-15 08:14:34 +00:00

Compare commits

...

6 Commits

3 changed files with 128 additions and 68 deletions

90
all.sas
View File

@@ -13393,7 +13393,7 @@ run;
@li deleted rows - these are re-inserted @li deleted rows - these are re-inserted
@li changed rows - differences are reverted @li changed rows - differences are reverted
@li added rows - these are marked with `_____DELETE_THIS_RECORD_____="YES"` @li added rows - marked with `_____DELETE__THIS__RECORD_____="YES"`
These changes are NOT applied to the base table - a staging dataset is These changes are NOT applied to the base table - a staging dataset is
simply prepared for an ETL process to action. In Data Controller, this simply prepared for an ETL process to action. In Data Controller, this
@@ -13407,15 +13407,19 @@ run;
change, plus ALL SUBSEQUENT CHANGES, will be reverted in the output table. change, plus ALL SUBSEQUENT CHANGES, will be reverted in the output table.
@param [in] difftable The dataset containing the diffs. Definition available @param [in] difftable The dataset containing the diffs. Definition available
in mddl_dc_difftable.sas in mddl_dc_difftable.sas
@param [in] filtervar= (0) If provided, the contents of this macro variable
will be applied as an additional filter against &libds
@param [out] outds= (work.mp_stripdiffs) Output table containing the diffs. @param [out] outds= (work.mp_stripdiffs) Output table containing the diffs.
Has the same format as the base datset, plus a Has the same format as the base datset, plus a
`_____DELETE_THIS_RECORD_____` variable. `_____DELETE__THIS__RECORD_____` variable.
@param [in] mdebug= set to 1 to enable DEBUG messages and preserve outputs @param [in] mdebug= set to 1 to enable DEBUG messages and preserve outputs
<h4> SAS Macros </h4> <h4> SAS Macros </h4>
@li mf_getuniquefileref.sas @li mf_getuniquefileref.sas
@li mf_getuniquename.sas @li mf_getuniquename.sas
@li mf_getvarlist.sas
@li mf_islibds.sas @li mf_islibds.sas
@li mf_wordsinstr1butnotstr2.sas
@li mp_abort.sas @li mp_abort.sas
<h4> Related Macros </h4> <h4> Related Macros </h4>
@@ -13432,6 +13436,7 @@ run;
%macro mp_stripdiffs(libds %macro mp_stripdiffs(libds
,loadref ,loadref
,difftable ,difftable
,filtervar=0
,outds=work.mp_stripdiffs ,outds=work.mp_stripdiffs
,mdebug=0 ,mdebug=0
)/*/STORE SOURCE*/; )/*/STORE SOURCE*/;
@@ -13455,11 +13460,11 @@ run;
,msg=%str(Invalid library.dataset reference - %superq(libds)) ,msg=%str(Invalid library.dataset reference - %superq(libds))
) )
/* set up unique and temporary vars */ /* set up unique and temporary vars */
%local ds1 ds2 ds3 ds4 ds5 fref1; %local ds1 ds2 ds3 ds4 ds5 fref1 filterstr;
%let fref1=%mf_getuniquefileref(); %let fref1=%mf_getuniquefileref();
%if &filtervar ne 0 %then %let filterstr=%superq(&filtervar);
%else %let filterstr=%str(1=1);
/* get timestamp of the diff to be reverted */ /* get timestamp of the diff to be reverted */
%local ts; %local ts;
@@ -13482,30 +13487,28 @@ create table &ds1 (drop=libref dsn) as
/* extract key values only */ /* extract key values only */
%let ds2=%upcase(work.%mf_getuniquename(prefix=mpsd_pks)); %let ds2=%upcase(work.%mf_getuniquename(prefix=mpsd_pks));
%local keyhash processed;
%let keyhash=%upcase(%mf_getuniquename(prefix=mpsdvar_keyhash));
%let processed=%upcase(%mf_getuniquename(prefix=mpsdvar_processed));
create table &ds2 as create table &ds2 as
select key_hash, select key_hash as &keyhash,
tgtvar_nm, tgtvar_nm,
tgtvar_type, tgtvar_type,
coalescec(oldval_char,newval_char) as charval, coalescec(oldval_char,newval_char) as charval,
coalesce(oldval_num, newval_num) as numval, coalesce(oldval_num, newval_num) as numval,
processed_dttm processed_dttm as &processed
from &ds1 from &ds1
where is_pk=1 where is_pk=1
order by key_hash, processed_dttm; order by &keyhash, &processed;
/* grab pk values */ /* grab pk values */
%local pk; %local pk;
data _null_; select distinct upcase(tgtvar_nm) into: pk separated by ' ' from &ds2;
set &ds2;
by key_hash;
call symputx('pk',catx(' ',symget('pk'),tgtvar_nm),'l');
if last.key_hash then stop;
run;
%let ds3=%upcase(work.%mf_getuniquename(prefix=mpsd_keychar)); %let ds3=%upcase(work.%mf_getuniquename(prefix=mpsd_keychar));
proc transpose data=&ds2(where=(tgtvar_type='C')) proc transpose data=&ds2(where=(tgtvar_type='C'))
out=&ds3(drop=_name_); out=&ds3(drop=_name_);
by KEY_HASH; by &keyhash &processed;
id TGTVAR_NM; id TGTVAR_NM;
var charval; var charval;
run; run;
@@ -13513,7 +13516,7 @@ run;
%let ds4=%upcase(work.%mf_getuniquename(prefix=mpsd_keynum)); %let ds4=%upcase(work.%mf_getuniquename(prefix=mpsd_keynum));
proc transpose data=&ds2(where=(tgtvar_type='N')) proc transpose data=&ds2(where=(tgtvar_type='N'))
out=&ds4(drop=_name_); out=&ds4(drop=_name_);
by KEY_HASH; by &keyhash &processed;
id TGTVAR_NM; id TGTVAR_NM;
var numval; var numval;
run; run;
@@ -13521,20 +13524,32 @@ run;
%mp_ds2squeeze(&ds3,outds=&ds3) %mp_ds2squeeze(&ds3,outds=&ds3)
%mp_ds2squeeze(&ds4,outds=&ds4) %mp_ds2squeeze(&ds4,outds=&ds4)
/* now merge to get all key values and de-dup */
%let ds5=%upcase(work.%mf_getuniquename(prefix=mpsd_merged)); %let ds5=%upcase(work.%mf_getuniquename(prefix=mpsd_merged));
data &ds5; data &ds5;
length key_hash $32; length &keyhash $32 &processed 8;
merge &ds3 &ds4; merge &ds3 &ds4;
by key_hash; by &keyhash &processed;
if not missing(key_hash); if not missing(&keyhash);
run;
proc sort data=&ds5 nodupkey;
by &pk;
run; run;
/* join to base table for preliminary stage DS */ /* join to base table for preliminary stage DS */
proc sql; proc sql;
create table &outds as select "No " as _____DELETE_THIS_RECORD_____, create table &outds as select "No " as _____DELETE__THIS__RECORD_____
b.* %do x=1 %to %sysfunc(countw(&pk,%str( )));
,a.%scan(&pk,&x,%str( ))
%end;
%local notpkcols;
%let notpkcols=%upcase(%mf_getvarlist(&libds));
%let notpkcols=%mf_wordsinstr1butnotstr2(str1=&notpkcols,str2=&pk);
%do x=1 %to %sysfunc(countw(&notpkcols,%str( )));
,b.%scan(&notpkcols,&x,%str( ))
%end;
from &ds5 a from &ds5 a
inner join &libds b left join &libds (where=(&filterstr)) b
on 1=1 on 1=1
%do x=1 %to %sysfunc(countw(&pk,%str( ))); %do x=1 %to %sysfunc(countw(&pk,%str( )));
and a.%scan(&pk,&x,%str( ))=b.%scan(&pk,&x,%str( )) and a.%scan(&pk,&x,%str( ))=b.%scan(&pk,&x,%str( ))
@@ -13544,7 +13559,8 @@ create table &outds as select "No " as _____DELETE_THIS_RECORD_____,
/* create SAS code to apply to stage_ds */ /* create SAS code to apply to stage_ds */
data _null_; data _null_;
set &ds1; set &ds1;
file &fref1; file &fref1 lrecl=33000;
length charval $32767;
if _n_=1 then put 'proc sql noprint;'; if _n_=1 then put 'proc sql noprint;';
by descending processed_dttm key_hash is_pk; by descending processed_dttm key_hash is_pk;
if move_type='M' then do; if move_type='M' then do;
@@ -13553,7 +13569,8 @@ data _null_;
end; end;
if IS_PK=0 then do; if IS_PK=0 then do;
put " " tgtvar_nm '=' @@; put " " tgtvar_nm '=' @@;
charval=quote(cats(oldval_char)); cnt=count(oldval_char,'"');
charval=quote(trim(substr(oldval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@; if tgtvar_type='C' then put charval @@;
else put oldval_num @@; else put oldval_num @@;
if not last.is_pk then put ','; if not last.is_pk then put ',';
@@ -13561,36 +13578,49 @@ data _null_;
else do; else do;
if first.is_pk then put " where 1=1 " @@; if first.is_pk then put " where 1=1 " @@;
put " and " tgtvar_nm '=' @@; put " and " tgtvar_nm '=' @@;
charval=quote(cats(oldval_char)); cnt=count(oldval_char,'"');
charval=quote(trim(substr(oldval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@; if tgtvar_type='C' then put charval @@;
else put oldval_num @@; else put oldval_num @@;
end; end;
end; end;
else if move_type='A' then do; else if move_type='A' then do;
if first.key_hash then do; if first.key_hash then do;
put "update &outds set _____DELETE_THIS_RECORD_____='Yes' where 1=1 " @@; put "update &outds set _____DELETE__THIS__RECORD_____='Yes' where 1=1 "@@;
end; end;
/* gating if - as only need PK now */ /* gating if - as only need PK now */
if is_pk=1; if is_pk=1;
put ' AND ' tgtvar_nm '=' @@; put ' AND ' tgtvar_nm '=' @@;
charval=quote(cats(newval_char)); cnt=count(newval_char,'"');
charval=quote(trim(substr(newval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@; if tgtvar_type='C' then put charval @@;
else put newval_num @@; else put newval_num @@;
end; end;
else if move_type='D' then do; else if move_type='D' then do;
if first.key_hash then do; if first.key_hash then do;
put "insert into &outds set _____DELETE_THIS_RECORD_____='No' " @@; put "update &outds set _____DELETE__THIS__RECORD_____='No' " @@;
end; end;
if IS_PK=0 then do;
put " ," tgtvar_nm '=' @@; put " ," tgtvar_nm '=' @@;
charval=quote(cats(oldval_char)); cnt=count(oldval_char,'"');
charval=quote(trim(substr(oldval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@; if tgtvar_type='C' then put charval @@;
else put oldval_num @@; else put oldval_num @@;
end; end;
else do;
if first.is_pk then put " where 1=1 " @@;
put " and " tgtvar_nm '=' @@;
cnt=count(oldval_char,'"');
charval=quote(trim(substr(oldval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@;
else put oldval_num @@;
end;
end;
if last.key_hash then put ';'; if last.key_hash then put ';';
run; run;
/* apply the modification statements */ /* apply the modification statements */
%inc &fref1/source2; %inc &fref1/source2 lrecl=33000;
%if &mdebug=0 %then %do; %if &mdebug=0 %then %do;
proc sql; proc sql;

View File

@@ -8,7 +8,7 @@
@li deleted rows - these are re-inserted @li deleted rows - these are re-inserted
@li changed rows - differences are reverted @li changed rows - differences are reverted
@li added rows - these are marked with `_____DELETE_THIS_RECORD_____="YES"` @li added rows - marked with `_____DELETE__THIS__RECORD_____="YES"`
These changes are NOT applied to the base table - a staging dataset is These changes are NOT applied to the base table - a staging dataset is
simply prepared for an ETL process to action. In Data Controller, this simply prepared for an ETL process to action. In Data Controller, this
@@ -22,15 +22,19 @@
change, plus ALL SUBSEQUENT CHANGES, will be reverted in the output table. change, plus ALL SUBSEQUENT CHANGES, will be reverted in the output table.
@param [in] difftable The dataset containing the diffs. Definition available @param [in] difftable The dataset containing the diffs. Definition available
in mddl_dc_difftable.sas in mddl_dc_difftable.sas
@param [in] filtervar= (0) If provided, the contents of this macro variable
will be applied as an additional filter against &libds
@param [out] outds= (work.mp_stripdiffs) Output table containing the diffs. @param [out] outds= (work.mp_stripdiffs) Output table containing the diffs.
Has the same format as the base datset, plus a Has the same format as the base datset, plus a
`_____DELETE_THIS_RECORD_____` variable. `_____DELETE__THIS__RECORD_____` variable.
@param [in] mdebug= set to 1 to enable DEBUG messages and preserve outputs @param [in] mdebug= set to 1 to enable DEBUG messages and preserve outputs
<h4> SAS Macros </h4> <h4> SAS Macros </h4>
@li mf_getuniquefileref.sas @li mf_getuniquefileref.sas
@li mf_getuniquename.sas @li mf_getuniquename.sas
@li mf_getvarlist.sas
@li mf_islibds.sas @li mf_islibds.sas
@li mf_wordsinstr1butnotstr2.sas
@li mp_abort.sas @li mp_abort.sas
<h4> Related Macros </h4> <h4> Related Macros </h4>
@@ -47,6 +51,7 @@
%macro mp_stripdiffs(libds %macro mp_stripdiffs(libds
,loadref ,loadref
,difftable ,difftable
,filtervar=0
,outds=work.mp_stripdiffs ,outds=work.mp_stripdiffs
,mdebug=0 ,mdebug=0
)/*/STORE SOURCE*/; )/*/STORE SOURCE*/;
@@ -70,11 +75,11 @@
,msg=%str(Invalid library.dataset reference - %superq(libds)) ,msg=%str(Invalid library.dataset reference - %superq(libds))
) )
/* set up unique and temporary vars */ /* set up unique and temporary vars */
%local ds1 ds2 ds3 ds4 ds5 fref1; %local ds1 ds2 ds3 ds4 ds5 fref1 filterstr;
%let fref1=%mf_getuniquefileref(); %let fref1=%mf_getuniquefileref();
%if &filtervar ne 0 %then %let filterstr=%superq(&filtervar);
%else %let filterstr=%str(1=1);
/* get timestamp of the diff to be reverted */ /* get timestamp of the diff to be reverted */
%local ts; %local ts;
@@ -97,30 +102,28 @@ create table &ds1 (drop=libref dsn) as
/* extract key values only */ /* extract key values only */
%let ds2=%upcase(work.%mf_getuniquename(prefix=mpsd_pks)); %let ds2=%upcase(work.%mf_getuniquename(prefix=mpsd_pks));
%local keyhash processed;
%let keyhash=%upcase(%mf_getuniquename(prefix=mpsdvar_keyhash));
%let processed=%upcase(%mf_getuniquename(prefix=mpsdvar_processed));
create table &ds2 as create table &ds2 as
select key_hash, select key_hash as &keyhash,
tgtvar_nm, tgtvar_nm,
tgtvar_type, tgtvar_type,
coalescec(oldval_char,newval_char) as charval, coalescec(oldval_char,newval_char) as charval,
coalesce(oldval_num, newval_num) as numval, coalesce(oldval_num, newval_num) as numval,
processed_dttm processed_dttm as &processed
from &ds1 from &ds1
where is_pk=1 where is_pk=1
order by key_hash, processed_dttm; order by &keyhash, &processed;
/* grab pk values */ /* grab pk values */
%local pk; %local pk;
data _null_; select distinct upcase(tgtvar_nm) into: pk separated by ' ' from &ds2;
set &ds2;
by key_hash;
call symputx('pk',catx(' ',symget('pk'),tgtvar_nm),'l');
if last.key_hash then stop;
run;
%let ds3=%upcase(work.%mf_getuniquename(prefix=mpsd_keychar)); %let ds3=%upcase(work.%mf_getuniquename(prefix=mpsd_keychar));
proc transpose data=&ds2(where=(tgtvar_type='C')) proc transpose data=&ds2(where=(tgtvar_type='C'))
out=&ds3(drop=_name_); out=&ds3(drop=_name_);
by KEY_HASH; by &keyhash &processed;
id TGTVAR_NM; id TGTVAR_NM;
var charval; var charval;
run; run;
@@ -128,7 +131,7 @@ run;
%let ds4=%upcase(work.%mf_getuniquename(prefix=mpsd_keynum)); %let ds4=%upcase(work.%mf_getuniquename(prefix=mpsd_keynum));
proc transpose data=&ds2(where=(tgtvar_type='N')) proc transpose data=&ds2(where=(tgtvar_type='N'))
out=&ds4(drop=_name_); out=&ds4(drop=_name_);
by KEY_HASH; by &keyhash &processed;
id TGTVAR_NM; id TGTVAR_NM;
var numval; var numval;
run; run;
@@ -136,20 +139,32 @@ run;
%mp_ds2squeeze(&ds3,outds=&ds3) %mp_ds2squeeze(&ds3,outds=&ds3)
%mp_ds2squeeze(&ds4,outds=&ds4) %mp_ds2squeeze(&ds4,outds=&ds4)
/* now merge to get all key values and de-dup */
%let ds5=%upcase(work.%mf_getuniquename(prefix=mpsd_merged)); %let ds5=%upcase(work.%mf_getuniquename(prefix=mpsd_merged));
data &ds5; data &ds5;
length key_hash $32; length &keyhash $32 &processed 8;
merge &ds3 &ds4; merge &ds3 &ds4;
by key_hash; by &keyhash &processed;
if not missing(key_hash); if not missing(&keyhash);
run;
proc sort data=&ds5 nodupkey;
by &pk;
run; run;
/* join to base table for preliminary stage DS */ /* join to base table for preliminary stage DS */
proc sql; proc sql;
create table &outds as select "No " as _____DELETE_THIS_RECORD_____, create table &outds as select "No " as _____DELETE__THIS__RECORD_____
b.* %do x=1 %to %sysfunc(countw(&pk,%str( )));
,a.%scan(&pk,&x,%str( ))
%end;
%local notpkcols;
%let notpkcols=%upcase(%mf_getvarlist(&libds));
%let notpkcols=%mf_wordsinstr1butnotstr2(str1=&notpkcols,str2=&pk);
%do x=1 %to %sysfunc(countw(&notpkcols,%str( )));
,b.%scan(&notpkcols,&x,%str( ))
%end;
from &ds5 a from &ds5 a
inner join &libds b left join &libds (where=(&filterstr)) b
on 1=1 on 1=1
%do x=1 %to %sysfunc(countw(&pk,%str( ))); %do x=1 %to %sysfunc(countw(&pk,%str( )));
and a.%scan(&pk,&x,%str( ))=b.%scan(&pk,&x,%str( )) and a.%scan(&pk,&x,%str( ))=b.%scan(&pk,&x,%str( ))
@@ -159,7 +174,8 @@ create table &outds as select "No " as _____DELETE_THIS_RECORD_____,
/* create SAS code to apply to stage_ds */ /* create SAS code to apply to stage_ds */
data _null_; data _null_;
set &ds1; set &ds1;
file &fref1; file &fref1 lrecl=33000;
length charval $32767;
if _n_=1 then put 'proc sql noprint;'; if _n_=1 then put 'proc sql noprint;';
by descending processed_dttm key_hash is_pk; by descending processed_dttm key_hash is_pk;
if move_type='M' then do; if move_type='M' then do;
@@ -168,7 +184,8 @@ data _null_;
end; end;
if IS_PK=0 then do; if IS_PK=0 then do;
put " " tgtvar_nm '=' @@; put " " tgtvar_nm '=' @@;
charval=quote(cats(oldval_char)); cnt=count(oldval_char,'"');
charval=quote(trim(substr(oldval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@; if tgtvar_type='C' then put charval @@;
else put oldval_num @@; else put oldval_num @@;
if not last.is_pk then put ','; if not last.is_pk then put ',';
@@ -176,36 +193,49 @@ data _null_;
else do; else do;
if first.is_pk then put " where 1=1 " @@; if first.is_pk then put " where 1=1 " @@;
put " and " tgtvar_nm '=' @@; put " and " tgtvar_nm '=' @@;
charval=quote(cats(oldval_char)); cnt=count(oldval_char,'"');
charval=quote(trim(substr(oldval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@; if tgtvar_type='C' then put charval @@;
else put oldval_num @@; else put oldval_num @@;
end; end;
end; end;
else if move_type='A' then do; else if move_type='A' then do;
if first.key_hash then do; if first.key_hash then do;
put "update &outds set _____DELETE_THIS_RECORD_____='Yes' where 1=1 " @@; put "update &outds set _____DELETE__THIS__RECORD_____='Yes' where 1=1 "@@;
end; end;
/* gating if - as only need PK now */ /* gating if - as only need PK now */
if is_pk=1; if is_pk=1;
put ' AND ' tgtvar_nm '=' @@; put ' AND ' tgtvar_nm '=' @@;
charval=quote(cats(newval_char)); cnt=count(newval_char,'"');
charval=quote(trim(substr(newval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@; if tgtvar_type='C' then put charval @@;
else put newval_num @@; else put newval_num @@;
end; end;
else if move_type='D' then do; else if move_type='D' then do;
if first.key_hash then do; if first.key_hash then do;
put "insert into &outds set _____DELETE_THIS_RECORD_____='No' " @@; put "update &outds set _____DELETE__THIS__RECORD_____='No' " @@;
end; end;
if IS_PK=0 then do;
put " ," tgtvar_nm '=' @@; put " ," tgtvar_nm '=' @@;
charval=quote(cats(oldval_char)); cnt=count(oldval_char,'"');
charval=quote(trim(substr(oldval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@; if tgtvar_type='C' then put charval @@;
else put oldval_num @@; else put oldval_num @@;
end; end;
else do;
if first.is_pk then put " where 1=1 " @@;
put " and " tgtvar_nm '=' @@;
cnt=count(oldval_char,'"');
charval=quote(trim(substr(oldval_char,1,32765-cnt)));
if tgtvar_type='C' then put charval @@;
else put oldval_num @@;
end;
end;
if last.key_hash then put ';'; if last.key_hash then put ';';
run; run;
/* apply the modification statements */ /* apply the modification statements */
%inc &fref1/source2; %inc &fref1/source2 lrecl=33000;
%if &mdebug=0 %then %do; %if &mdebug=0 %then %do;
proc sql; proc sql;

View File

@@ -82,10 +82,10 @@ run;
%let addpass=0; %let addpass=0;
data _null_; data _null_;
set work.mp_stripdiffs; set work.mp_stripdiffs;
if upcase(_____DELETE_THIS_RECORD_____)='NO' and name="&delname" if upcase(_____DELETE__THIS__RECORD_____)='NO' and name="&delname"
then call symputx('delpass',1); then call symputx('delpass',1);
if name="&modname" and age=&modval then call symputx('modpass',1); if name="&modname" and age=&modval then call symputx('modpass',1);
if upcase(_____DELETE_THIS_RECORD_____)='YES' and name="Newbie" if upcase(_____DELETE__THIS__RECORD_____)='YES' and name="Newbie"
then call symputx('addpass',1); then call symputx('addpass',1);
run; run;