Skip to content

Commit

Permalink
feat: mp_appendfile macro for appending 2 or more files together
Browse files Browse the repository at this point in the history
  • Loading branch information
Allan Bowe committed Sep 27, 2021
1 parent dae03c5 commit 52dfa7b
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 2 deletions.
58 changes: 57 additions & 1 deletion all.sas
Expand Up @@ -1856,7 +1856,7 @@ Usage:

%if "&sysprocessmode " = "SAS Stored Process Server " %then %do;
data _null_;
putlog 'stpsrvset program error and syscc';
putlog 'stpsrvset program err and syscc';
rc=stpsrvset('program error', 0);
call symputx("syscc",0,"g");
run;
Expand Down Expand Up @@ -1902,6 +1902,62 @@ Usage:
%mend mp_abort;

/** @endcond *//**
@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;/**
@file
@brief Generic assertion
@details Useful in the context of writing sasjs tests. The results of the
Expand Down
2 changes: 1 addition & 1 deletion base/mp_abort.sas
Expand Up @@ -208,7 +208,7 @@

%if "&sysprocessmode " = "SAS Stored Process Server " %then %do;
data _null_;
putlog 'stpsrvset program error and syscc';
putlog 'stpsrvset program err and syscc';
rc=stpsrvset('program error', 0);
call symputx("syscc",0,"g");
run;
Expand Down
57 changes: 57 additions & 0 deletions base/mp_appendfile.sas
@@ -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;
41 changes: 41 additions & 0 deletions tests/crossplatform/mp_appendfile.test.sas
@@ -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
)

0 comments on commit 52dfa7b

Please sign in to comment.