Production Ready Macros for SAS Application Developers
https://github.com/sasjs/core
mp_gsubfile.test.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_gsubfile.sas macro
4
5 <h4> SAS Macros </h4>
6 @li mp_gsubfile.sas
7 @li mp_assert.sas
8
9**/
10
11/**
12 * test 1 - simple replace
13 */
14%global str1;
15%let file=%sysfunc(pathname(work))/file.txt;
16%let pat=replace/me;
17%let str=with/this;
18data _null_;
19 file "&file";
20 put "&pat";
21run;
22%mp_gsubfile(file=&file, patternvar=pat, replacevar=str)
23data _null_;
24 infile "&file";
25 input;
26 call symputx('str1',_infile_);
27run;
28
29%mp_assert(
30 iftrue=("&str1"="&str"),
31 desc=Check that simple replacement was successful,
32 outds=work.test_results
33)
34
35/**
36 * test 2 - replace from additional line
37 */
38%global str2 strcheck2 strcheck2b;
39%let file2=%sysfunc(pathname(work))/file2.txt;
40%let pat2=replace/me;
41%let str2=with/this;
42data _null_;
43 file "&file2";
44 put 'line1';output;
45 put "&pat2";output;
46 put "&pat2";output;
47run;
48%mp_gsubfile(file=&file2, patternvar=pat2, replacevar=str2)
49data _null_;
50 infile "&file2";
51 input;
52 if _n_=2 then call symputx('strcheck2',_infile_);
53 if _n_=3 then call symputx('strcheck2b',_infile_);
54 putlog _infile_;
55run;
56
57%mp_assert(
58 iftrue=("&strcheck2"="&str2"),
59 desc=Check that multi line replacement was successful (line2),
60 outds=work.test_results
61)
62%mp_assert(
63 iftrue=("&strcheck2b"="&str2"),
64 desc=Check that multi line replacement was successful (line3),
65 outds=work.test_results
66)