Macros for SAS Application Developers
https://github.com/sasjs/core
Loading...
Searching...
No Matches
mp_ds2csv.test.1.sas
Go to the documentation of this file.
1/**
2 @file
3 @brief Testing mp_ds2csv.sas macro
4
5 <h4> SAS Macros </h4>
6 @li mp_ds2csv.sas
7 @li mp_assert.sas
8 @li mp_assertscope.sas
9
10**/
11
12data work.somedata;
13 x=1;
14 y=' t"w"o';
15 z=.z;
16 label x='x factor';
17run;
18
19/**
20 * Test 1 - default CSV
21 */
22%mp_assertscope(SNAPSHOT)
23%mp_ds2csv(work.somedata,outfile="&sasjswork/test1.csv")
24%mp_assertscope(COMPARE)
25
26%let test1b=FAIL;
27data _null_;
28 infile "&sasjswork/test1.csv";
29 input;
30 list;
31 if _n_=1 then call symputx('test1a',_infile_);
32 else if _infile_=:'1," t""w""o",.Z' then call symputx('test1b','PASS');
33run;
34
35%mp_assert(
36 iftrue=("&test1a"="x factor, Y, Z"),
37 desc=Checking header row Test 1,
38 outds=work.test_results
39)
40%mp_assert(
41 iftrue=("&test1b"="PASS"),
42 desc=Checking data row Test 1,
43 outds=work.test_results
44)
45
46/**
47 * Test 2 - NAME header with fileref and semicolons
48 */
49filename test2 "&sasjswork/test2.csv";
50%mp_ds2csv(work.somedata,outref=test2,dlm=SEMICOLON,headerformat=NAME)
51
52%let test2b=FAIL;
53data _null_;
54 infile test2;
55 input;
56 list;
57 if _n_=1 then call symputx('test2a',_infile_);
58 else if _infile_=:'1;" t""w""o";.Z' then call symputx('test2b','PASS');
59run;
60
61%mp_assert(
62 iftrue=("&test2a"="X; Y; Z"),
63 desc=Checking header row Test 2,
64 outds=work.test_results
65)
66%mp_assert(
67 iftrue=("&test2b"="PASS"),
68 desc=Checking data row Test 2,
69 outds=work.test_results
70)
71
72/**
73 * Test 3 - SASjs format
74 */
75filename test3 "&sasjswork/test3.csv";
76%mp_ds2csv(work.somedata,outref=test3,headerformat=SASJS)
77
78%let test3b=FAIL;
79data _null_;
80 infile test3;
81 input;
82 list;
83 if _n_=1 then call symputx('test3a',_infile_);
84 else if _infile_=:'1," t""w""o",.Z' then call symputx('test3b','PASS');
85run;
86
87%mp_assert(
88 iftrue=("&test3a"="X:best32. Y:$char7. Z:best32."),
89 desc=Checking header row Test 3,
90 outds=work.test_results
91)
92%mp_assert(
93 iftrue=("&test3b"="PASS"),
94 desc=Checking data row Test 3,
95 outds=work.test_results
96)
97
98/* test 4 - sasjs with compare */
99data work.baseball ;
100attrib
101Name length= $18 label="Player's Name"
102Team length= $14 label="Team at the End of 1986"
103nAtBat length= 8 label="Times at Bat in 1986"
104nHits length= 8 label="Hits in 1986"
105nHome length= 8 label="Home Runs in 1986"
106nRuns length= 8 label="Runs in 1986"
107nRBI length= 8 label="RBIs in 1986"
108nBB length= 8 label="Walks in 1986"
109YrMajor length= 8 label="Years in the Major Leagues"
110CrAtBat length= 8 label="Career Times at Bat"
111CrHits length= 8 label="Career Hits"
112CrHome length= 8 label="Career Home Runs"
113CrRuns length= 8 label="Career Runs"
114CrRbi length= 8 label="Career RBIs"
115CrBB length= 8 label="Career Walks"
116League length= $8 label="League at the End of 1986"
117Division length= $8 label="Division at the End of 1986"
118Position length= $8 label="Position(s) in 1986"
119nOuts length= 8 label="Put Outs in 1986"
120nAssts length= 8 label="Assists in 1986"
121nError length= 8 label="Errors in 1986"
122Salary length= 8 label="1987 Salary in $ Thousands"
123Div length= $16 label="League and Division"
124logSalary length= 8 label="Log Salary"
125;
126infile cards dsd;
127input
128 Name :$char.
129 Team :$char.
130 nAtBat
131 nHits
132 nHome
133 nRuns
134 nRBI
135 nBB
136 YrMajor
137 CrAtBat
138 CrHits
139 CrHome
140 CrRuns
141 CrRbi
142 CrBB
143 League :$char.
144 Division :$char.
145 Position :$char.
146 nOuts
147 nAssts
148 nError
149 Salary
150 Div :$char.
151 logSalary
152;
153missing a b c d e f g h i j k l m n o p q r s t u v w x y z _;
154/* fix precision issues */
155if not missing(logSalary) then logsalary=logSalary*1;
156datalines4;
157"Allanson, Andy",Cleveland,293,66,1,30,29,14,1,293,66,1,30,29,14,American,East,C,446,33,20,.,AE,.
158"Ashby, Alan",Houston,315,81,7,24,38,39,14,3449,835,69,321,414,375,National,West,C,632,43,10,475,NW,6.16331480403464
159"Davis, Alan",Seattle,479,130,18,66,72,76,3,1624,457,63,224,266,263,American,West,1B,880,82,14,480,AW,6.17378610390193
160"Dawson, Andre",Montreal,496,141,20,65,78,37,11,5628,1575,225,828,838,354,National,East,RF,200,11,3,500,NE,6.21460809842219
161"Galarraga, Andres",Montreal,321,87,10,39,42,30,2,396,101,12,48,46,33,National,East,1B,805,40,4,91.5,NE,4.51633897228147
162"Griffin, Alfredo",Oakland,594,169,4,74,51,35,11,4408,1133,19,501,336,194,American,West,SS,282,421,25,750,AW,6.62007320653035
163"Newman, Al",Montreal,185,37,1,23,8,21,2,214,42,1,30,9,24,National,East,2B,76,127,7,70,NE,4.24849524204936
164"Salazar, Argenis",Kansas City,298,73,0,24,24,7,3,509,108,0,41,37,12,American,West,SS,121,283,9,100,AW,4.60517018598809
165"Thomas, Andres",Atlanta,323,81,6,26,32,8,2,341,86,6,32,34,8,National,West,SS,143,290,19,75,NW,4.31748811353631
166"Thornton, Andre",Cleveland,401,92,17,49,66,65,13,5206,1332,253,784,890,866,American,East,DH,0,0,0,1100,AE,7.00306545878646
167"Trammell, Alan",Detroit,574,159,21,107,75,59,10,4631,1300,90,702,504,488,American,East,SS,238,445,22,517.143,AE,6.24831943200756
168"Trevino, Alex",Los Angeles,202,53,4,31,26,27,9,1876,467,15,192,186,161,National,West,C,304,45,11,512.5,NW,6.23930071101256
169"Van Slyke, Andy",St Louis,418,113,13,48,61,47,4,1512,392,41,205,204,203,National,East,RF,211,11,7,550,NE,6.30991827822651
170"Wiggins, Alan",Baltimore,239,60,0,30,11,22,6,1941,510,4,309,103,207,American,East,2B,121,151,6,700,AE,6.5510803350434
171"Almon, Bill",Pittsburgh,196,43,7,29,27,30,13,3231,825,36,376,290,238,National,East,UT,80,45,8,240,NE,5.48063892334199
172"Beane, Billy",Minneapolis,183,39,3,20,15,11,3,201,42,3,20,16,11,American,West,OF,118,0,0,.,AW,.
173"Bell, Buddy",Cincinnati,568,158,20,89,75,73,15,8068,2273,177,1045,993,732,National,West,3B,105,290,10,775,NW,6.65286302935334
174"Biancalana, Buddy",Kansas City,190,46,2,24,8,15,5,479,102,5,65,23,39,American,West,SS,102,177,16,175,AW,5.16478597392351
175"Bochte, Bruce",Oakland,407,104,6,57,43,65,12,5233,1478,100,643,658,653,American,West,1B,912,88,9,.,AW,.
176;;;;
177run;
178filename example temp lrecl=5000;
179%mp_ds2csv(work.baseball,outref=example,headerformat=SASJS)
180data _null_; infile example; input;put _infile_; if _n_>5 then stop;run;
181
182data _null_;
183 infile example;
184 input;
185 call symputx('stmnt',_infile_);
186 stop;
187run;
188data work.want;
189 infile example dsd firstobs=2;
190 input &stmnt;
191run;
192
193%mp_assert(
194 iftrue=(&syscc =0),
195 desc=Checking syscc prior to compare of work.baseball,
196 outds=work.test_results
197)
198
199proc compare base=want compare=work.baseball
200 method = absolute criterion = 0.0000000001 ;
201run;
202%mp_assert(
203 iftrue=(&sysinfo le 41),
204 desc=Checking compare of work.baseball,
205 outds=work.test_results
206)
207
208/* test 5 - sasjs with time/datetime/date */
209filename f2 temp;
210data work.test5;
211 do x=1 to 5;
212 y=x;
213 z=x;
214 end;
215 format x date9. y datetime19. z time.;
216run;
217%mp_ds2csv(work.test5,outref=f2,headerformat=SASJS)
218data _null_; infile example; input;put _infile_; if _n_>5 then stop;run;
219
220data _null_;
221 infile f2;
222 input;
223 putlog _infile_;
224 call symputx('stmnt2',_infile_);
225 stop;
226run;
227data work.want5;
228 infile f2 dsd firstobs=2;
229 input &stmnt2;
230 putlog _infile_;
231run;
232
233%mp_assert(
234 iftrue=(&syscc=0),
235 desc=Checking syscc prior to compare of test5,
236 outds=work.test_results
237)
238
239proc compare base=want5 compare=work.test5;
240run;
241%mp_assert(
242 iftrue=(&sysinfo le 41),
243 desc=Checking compare of work.test5,
244 outds=work.test_results
245)
246