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 %macro gsubtest();
13 %if "%substr(&sysver.XX,1,4)"="V.04" %then %do;
14  %put %str(ERR)OR: Viya 4 does not support the IO library in lua;
15  %return;
16 %end;
17 
18 /**
19  * test 1 - simple replace
20  */
21 %global str1;
22 %let file=%sysfunc(pathname(work))/file.txt;
23 %let pat=replace/me;
24 %let str=with/this;
25 data _null_;
26  file "&file";
27  put "&pat";
28 run;
29 %mp_gsubfile(file=&file, patternvar=pat, replacevar=str)
30 data _null_;
31  infile "&file";
32  input;
33  call symputx('str1',_infile_);
34 run;
35 
36 %mp_assert(
37  iftrue=("&str1"="&str"),
38  desc=Check that simple replacement was successful,
39  outds=work.test_results
40 )
41 
42 /**
43  * test 2 - replace from additional line
44  */
45 %global str2 strcheck2 strcheck2b;
46 %let file2=%sysfunc(pathname(work))/file2.txt;
47 %let pat2=replace/me;
48 %let str2=with/this;
49 data _null_;
50  file "&file2";
51  put 'line1';output;
52  put "&pat2";output;
53  put "&pat2";output;
54 run;
55 %mp_gsubfile(file=&file2, patternvar=pat2, replacevar=str2)
56 data _null_;
57  infile "&file2";
58  input;
59  if _n_=2 then call symputx('strcheck2',_infile_);
60  if _n_=3 then call symputx('strcheck2b',_infile_);
61  putlog _infile_;
62 run;
63 
64 %mp_assert(
65  iftrue=("&strcheck2"="&str2"),
66  desc=Check that multi line replacement was successful (line2),
67  outds=work.test_results
68 )
69 %mp_assert(
70  iftrue=("&strcheck2b"="&str2"),
71  desc=Check that multi line replacement was successful (line3),
72  outds=work.test_results
73 )
74 
75 %mend gsubtest;
76 
77 %gsubtest()