Macros for SAS Application Developers
https://github.com/sasjs/core
mp_binarycopy.test.sas
Go to the documentation of this file.
1 /**
2  @file
3  @brief Testing mp_binarycopy.sas macro
4 
5  <h4> SAS Macros </h4>
6  @li mp_binarycopy.sas
7  @li mp_assert.sas
8  @li mp_hashdataset.sas
9 
10 **/
11 
12 
13 /* TEST 1 - regular file copy */
14 %let string1=test1;
15 filename tmp temp;
16 filename myref temp;
17 data _null_;
18  file tmp;
19  put "&string1";
20 run;
21 %mp_binarycopy(inref=tmp, outref=myref)
22 data _null_;
23  infile myref;
24  input;
25  put _infile_;
26  call symputx('string1_check',_infile_);
27  stop;
28 run;
29 %mp_assert(
30  iftrue=("&string1"="&string1_check"),
31  desc=Basic String Compare,
32  outds=work.test_results
33 )
34 
35 
36 /* TEST 2 - File append */
37 %let string2=test2;
38 %let path2=%sysfunc(pathname(work))/somefile.txt;
39 data _null_;
40  file "&path2";
41  put "&string2";
42 run;
43 %mp_binarycopy(inloc="&path2", outref=myref, mode=APPEND)
44 data _null_;
45  infile myref;
46  input;
47  put _infile_;
48  if _n_=2 then call symputx('string2_check',_infile_);
49 run;
50 %mp_assert(
51  iftrue=("&string2"="&string2_check"),
52  desc=Append Check (file to ref),
53  outds=work.test_results
54 )
55 
56 /* TEST 3 - File create (ref to existing file) */
57 %let string3=test3;
58 %let path3=%sysfunc(pathname(work))/somefile3.txt;
59 filename tmp3 temp;
60 data _null_;
61  file tmp3;
62  put "&string3";
63 run;
64 data _null_;
65  file "&path3";
66  put "this should not be returned";
67 run;
68 %mp_binarycopy(inref=tmp3, outloc="&path3")
69 data _null_;
70  infile "&path3";
71  input;
72  put _infile_;
73  if _n_=1 then call symputx('string3_check',_infile_);
74 run;
75 %mp_assert(
76  iftrue=("&string3"="&string3_check"),
77  desc=Append Check (ref to existing file),
78  outds=work.test_results
79 )
80 
81 /* TEST 4 - File append (ref to file) */
82 %let string4=test4;
83 %let string4_check=;
84 filename tmp4 temp;
85 data _null_;
86  file tmp4;
87  put "&string4";
88 run;
89 %mp_binarycopy(inref=tmp4, outloc="&path3",mode=APPEND)
90 data _null_;
91  infile "&path3";
92  input;
93  put _infile_;
94  if _n_=2 then call symputx('string4_check',_infile_);
95 run;
96 %mp_assert(
97  iftrue=("&string4"="&string4_check"),
98  desc=Append Check (ref to file),
99  outds=work.test_results
100 )
101 
102 /* test 5 - ensure copy works for binary characters */
103 /* do this backwards to avoid null chars in JSON preview */
104 data work.test5;
105 do i=255 to 1 by -1;
106  str=byte(i);
107  output;
108 end;
109 run;
110 /* get an md5 hash of the ds */
111 %mp_hashdataset(work.test5,outds=myhash)
112 
113 /* copy it */
114 %mp_binarycopy(inloc="%sysfunc(pathname(work))/test5.sas7bdat",
115  outloc="%sysfunc(pathname(work))/test5copy.sas7bdat"
116 )
117 
118 /* get an md5 hash of the copied ds */
119 %mp_hashdataset(work.test5copy,outds=myhash2)
120 
121 /* compare hashes */
122 %let test5a=0;
123 %let test5b=1;
124 data _null_;
125  set myhash;
126  call symputx('test5a',hashkey);
127 run;
128 data _null_;
129  set myhash2;
130  call symputx('test5b',hashkey);
131 run;
132 %mp_assert(
133  iftrue=("&test5a"="&test5b"),
134  desc=Ensuring binary copy works on binary characters,
135  outds=work.test_results
136 )