Published on :
Test INTERNAL_CREATION

Test of the mf_writefile macro

This code is also available in: Deutsch Español Français
Awaiting validation
The program is structured as a series of tests. Initially, it uses `%mf_writefile` to create a file in the `&sasjswork` directory with two lines. A `DATA _null_` step then reads the second line to store it in a macro variable (`test1`). The `%mp_assert` macro is then used to check for errors and content compliance. A second call to `%mf_writefile` overwrites the file with modified content. The same read and assert steps are repeated to check for overwriting (`test2`). Finally, the script declares global macro variables (`test3`, `test4`) and calls `%mf_writefile` with the `mode=a` option to append new lines to the file. A final series of `DATA _null_` and `%mp_assert` validates that the previous lines have not been modified and that the new lines have been correctly appended.
Data Analysis

Type : INTERNAL_CREATION


Data is entirely generated and manipulated internally. The `%mf_writefile` macro creates a temporary text file (`&sasjswork/myfile.txt`) which serves as both source and destination for read and write operations. No data external to the script or from standard SAS libraries like SASHELP is used for business logic or tests.

1 Code Block
Macro %mf_writefile Data
Explanation :
This call to the `%mf_writefile` macro creates or overwrites the `myfile.txt` file in the temporary working library `&sasjswork`. It writes 'some content' as the first line and 'more content' as the second line.
Copied!
1%mf_writefile(&sasjswork/myfile.txt,l1=some content,l2=more content)
2 Code Block
DATA STEP
Explanation :
This `DATA _null_` block reads the content of the `&sasjswork/myfile.txt` file. It iterates through each line and, when the second line is reached (`_n_=2`), it stores its exact content in the macro variable `test1`.
Copied!
1DATA _null_;
2 INFILE "&sasjswork/myfile.txt";
3 INPUT;
4 IF _n_=2 THEN call symputx('test1',_infile_);
5RUN;
3 Code Block
Macro %mp_assert
Explanation :
These two calls to the `%mp_assert` macro are unit tests. The first verifies that the execution of the previous code did not generate any SAS errors (`&syscc=0`). The second ensures that the macro variable `test1` indeed contains the value 'more content', thus confirming that the second line of the file was correctly written and read.
Copied!
1%mp_assert(
2 iftrue=(&syscc=0),
3 desc=Check code ran without errors,
4 outds=work.test_results
5)
6%mp_assert(
7 iftrue=(&test1=more content),
8 desc=Checking line was created,
9 outds=work.test_results
10)
4 Code Block
Macro %mf_writefile Data
Explanation :
This call to the `%mf_writefile` macro overwrites the `&sasjswork/myfile.txt` file again. This time, the second line is set to 'different content', testing the macro's ability to replace existing content.
Copied!
1%mf_writefile(&sasjswork/myfile.txt,l1=some content,l2=different content)
5 Code Block
DATA STEP
Explanation :
Similar to the previous block, this `DATA _null_` reads the file after the overwrite operation. It extracts the second line and stores it in the macro variable `test2` for subsequent verification.
Copied!
1DATA _null_;
2 INFILE "&sasjswork/myfile.txt";
3 INPUT;
4 IF _n_=2 THEN call symputx('test2',_infile_);
5RUN;
6 Code Block
Macro %mp_assert
Explanation :
These assertions verify that the previous code executed without error. They also confirm that the macro variable `test2` contains 'different content', thus validating that the second line of the file was correctly overwritten by the new content.
Copied!
1%mp_assert(
2 iftrue=(&syscc=0),
3 desc=Check code ran without errors for test2,
4 outds=work.test_results
5)
6%mp_assert(
7 iftrue=(&test2=different content),
8 desc=Checking second line was overwritten,
9 outds=work.test_results
10)
7 Code Block
Macro %mf_writefile Data
Explanation :
This block declares macro variables `test3` and `test4` as global. Then, the `%mf_writefile` macro is called with the `mode=a` option, which indicates an append mode. The new lines 'aah, content' (where `%str` protects the comma) and 'append content' are appended to the end of the existing file without modifying the previous lines.
Copied!
1%global test3 test4;
2%mf_writefile(&sasjswork/myfile.txt
3 ,mode=a
4 ,l1=%str(aah, content)
5 ,l2=append content
6)
8 Code Block
DATA STEP
Explanation :
This `DATA _null_` reads the file after the append operation. The second line is read into `test3` to check its integrity (that it has not been modified). The fourth line, corresponding to the second appended line, is read into `test4` to check for correct content appending.
Copied!
1DATA _null_;
2 INFILE "&sasjswork/myfile.txt";
3 INPUT;
4 IF _n_=2 THEN call symputx('test3',_infile_);
5 IF _n_=4 THEN call symputx('test4',_infile_);
6RUN;
9 Code Block
Macro %mp_assert
Explanation :
These final assertions validate the append operation. They confirm the absence of errors, ensure that the content of the second line (`test3`) remained 'different content' (i.e., it was not overwritten by the append), and verify that the fourth line (`test4`) indeed contains 'append content', confirming successful appending.
Copied!
1%mp_assert(
2 iftrue=(&syscc=0),
3 desc=Check code ran without errors for test2,
4 outds=work.test_results
5)
6%mp_assert(
7 iftrue=(&test3=different content),
8 desc=Checking second line was not overwritten,
9 outds=work.test_results
10)
11%mp_assert(
12 iftrue=(&test4=append content),
13 desc=Checking fourth line was appended,
14 outds=work.test_results
15)
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.