Published on :
Macro INTERNAL_CREATION

Binary File Concatenation

This code is also available in: Deutsch Español Français
The `mp_appendfile` macro allows merging the content of one or more files specified by their 'filerefs' (file references) to the end of an existing base file, also designated by a 'fileref'. The operation is performed in binary mode, ensuring compatibility with any file type. The user is responsible for adding carriage returns if necessary in the files to be concatenated. The macro includes validations to ensure that the base and append 'filerefs' are properly provided. It iterates over each 'fileref' to be appended and uses the `%mp_binarycopy` macro to perform the copy, including a check of the system return code (`&syscc`) after each operation.
Data Analysis

Type : INTERNAL_CREATION


The macro does not depend on predefined SAS datasets or external files not managed by the script. It operates on file references ('filerefs') which can be associated with temporary files or files created and managed by the user within the SAS session. The usage example demonstrates the creation of temporary files ('temp') using DATA _NULL_ and FILENAME statements, indicating that the source data is controlled and/or generated within the SAS environment.

1 Code Block
Macro Definition and Parameter Validation
Explanation :
This block initializes the `mp_appendfile` macro and defines its two input parameters, `baseref` (the fileref of the destination file) and `appendrefs` (a list of filerefs of files to append). It includes calls to the `%mp_abort` macro to validate if these parameters are properly provided by the user. If a mandatory parameter is missing, the `%mp_abort` macro is triggered to interrupt execution and display a clear error message.
Copied!
1%macro mp_appendfile(
2 baseref=0,
3 appendrefs=0
4)/*/STORE SOURCE*/;
5 
6%mp_abort(iftrue= (&baseref=0)
7 ,mac=&sysmacroname
8 ,msg=%str(Baseref NOT specified!)
9)
10%mp_abort(iftrue= (&appendrefs=0)
11 ,mac=&sysmacroname
12 ,msg=%str(Appendrefs NOT specified!)
13)
2 Code Block
File Appending Loop
Explanation :
This block contains the main logic of the macro. A loop is used to iterate over each 'fileref' specified in the `appendrefs` parameter. In each iteration, a check of the system return code (`&syscc`) is performed via `%mp_abort` to ensure that no error occurred during the previous operation. Then, the `%mp_binarycopy` macro is called to copy the content of the current source file (`inref`) to the base file (`outref`) using `APPEND` mode, which allows appending the content to the end of the existing file.
Copied!
1%local i;
2%DO i=1 %to %sysfunc(countw(&appendrefs));
3 %mp_abort(iftrue= (&syscc>0)
4 ,mac=&sysmacroname
5 ,msg=%str(syscc=&syscc)
6 )
7 %mp_binarycopy(inref=%scan(&appendrefs,&i), outref=&baseref, mode=APPEND)
8%END;
9 
10%mend mp_appendfile;
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.
Copyright Info : Copyright (c) 2001-2006 Rodney Sparapani; Allan Bowe, source: https://github.com/sasjs/core