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.
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!
data _null_;
infile "&sasjswork/myfile.txt";
input;
if _n_=2 then call symputx('test1',_infile_);
run;
1
DATA _null_;
2
INFILE"&sasjswork/myfile.txt";
3
INPUT;
4
IF _n_=2THEN call symputx('test1',_infile_);
5
RUN;
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!
%mp_assert(
iftrue=(&syscc=0),
desc=Check code ran without errors,
outds=work.test_results
)
%mp_assert(
iftrue=(&test1=more content),
desc=Checking line was created,
outds=work.test_results
)
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.
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!
data _null_;
infile "&sasjswork/myfile.txt";
input;
if _n_=2 then call symputx('test2',_infile_);
run;
1
DATA _null_;
2
INFILE"&sasjswork/myfile.txt";
3
INPUT;
4
IF _n_=2THEN call symputx('test2',_infile_);
5
RUN;
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!
%mp_assert(
iftrue=(&syscc=0),
desc=Check code ran without errors for test2,
outds=work.test_results
)
%mp_assert(
iftrue=(&test2=different content),
desc=Checking second line was overwritten,
outds=work.test_results
)
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.
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!
data _null_;
infile "&sasjswork/myfile.txt";
input;
if _n_=2 then call symputx('test3',_infile_);
if _n_=4 then call symputx('test4',_infile_);
run;
1
DATA _null_;
2
INFILE"&sasjswork/myfile.txt";
3
INPUT;
4
IF _n_=2THEN call symputx('test3',_infile_);
5
IF _n_=4THEN call symputx('test4',_infile_);
6
RUN;
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!
%mp_assert(
iftrue=(&syscc=0),
desc=Check code ran without errors for test2,
outds=work.test_results
)
%mp_assert(
iftrue=(&test3=different content),
desc=Checking second line was not overwritten,
outds=work.test_results
)
%mp_assert(
iftrue=(&test4=append content),
desc=Checking fourth line was appended,
outds=work.test_results
)
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.
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. ® indicates USA registration. WeAreCAS is an independent community site and is not affiliated with SAS Institute Inc.
This site uses technical and analytical cookies to improve your experience.
Read more.