Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mp_appendfile macro for appending 2 or more files together
- Loading branch information
Allan Bowe
committed
Sep 27, 2021
1 parent
dae03c5
commit 52dfa7b
Showing
4 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
@file | ||
@brief Append (concatenate) two or more files. | ||
@details Will append one more more `appendrefs` (filerefs) to a `baseref`. | ||
Uses a binary mechanism, so will work with any file type. For that reason - | ||
use with care! And supply your own trailing carriage returns in each file.. | ||
Usage: | ||
filename tmp1 temp; | ||
filename tmp2 temp; | ||
filename tmp3 temp; | ||
data _null_; file tmp1; put 'base file'; | ||
data _null_; file tmp2; put 'append1'; | ||
data _null_; file tmp3; put 'append2'; | ||
run; | ||
%mp_appendfile(baseref=tmp1, appendrefs=tmp2 tmp3) | ||
@param [in] baseref= Fileref of the base file (should exist) | ||
@param [in] appendrefs= One or more filerefs to be appended to the base | ||
fileref. Space separated. | ||
@version 9.2 | ||
@author Allan Bowe, source: https://github.com/sasjs/core | ||
<h4> SAS Macros </h4> | ||
@li mp_abort.sas | ||
@li mp_binarycopy.sas | ||
**/ | ||
|
||
%macro mp_appendfile( | ||
baseref=0, | ||
appendrefs=0 | ||
)/*/STORE SOURCE*/; | ||
|
||
%mp_abort(iftrue= (&baseref=0) | ||
,mac=&sysmacroname | ||
,msg=%str(Baseref NOT specified!) | ||
) | ||
%mp_abort(iftrue= (&appendrefs=0) | ||
,mac=&sysmacroname | ||
,msg=%str(Appendrefs NOT specified!) | ||
) | ||
|
||
%local i; | ||
%do i=1 %to %sysfunc(countw(&appendrefs)); | ||
%mp_abort(iftrue= (&syscc>0) | ||
,mac=&sysmacroname | ||
,msg=%str(syscc=&syscc) | ||
) | ||
%mp_binarycopy(inref=%scan(&appendrefs,&i), outref=&baseref, mode=APPEND) | ||
%end; | ||
|
||
%mend mp_appendfile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
@file | ||
@brief Testing mp_appendfile.sas macro | ||
<h4> SAS Macros </h4> | ||
@li mp_appendfile.sas | ||
@li mp_assert.sas | ||
**/ | ||
|
||
|
||
filename tmp1 temp; | ||
filename tmp2 temp; | ||
filename tmp3 temp; | ||
data _null_; file tmp1; put 'base file'; | ||
data _null_; file tmp2; put 'append1'; | ||
data _null_; file tmp3; put 'append2'; | ||
run; | ||
%mp_appendfile(baseref=tmp1, appendrefs=tmp2 tmp3) | ||
data _null_; | ||
infile tmp1; | ||
input; | ||
put _infile_; | ||
call symputx(cats('check',_n_),_infile_); | ||
run; | ||
%global check1 check2 check3; | ||
%mp_assert( | ||
iftrue=("&check1"="base file"), | ||
desc=Line 1 of file tmp1 is correct, | ||
outds=work.test_results | ||
) | ||
%mp_assert( | ||
iftrue=("&check2"="append1"), | ||
desc=Line 2 of file tmp1 is correct, | ||
outds=work.test_results | ||
) | ||
%mp_assert( | ||
iftrue=("&check3"="append2"), | ||
desc=Line 3 of file tmp1 is correct, | ||
outds=work.test_results | ||
) |