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 @sasjslint {"noBannedStrings": false, "maxLineLength": 129}
11**/
12
13data work.somedata;
14 x=1;
15 y=' t"w"o';
16 z=.z;
17 label x='x factor';
18run;
19
20/**
21 * Test 1 - default CSV
22 */
23%mp_assertscope(SNAPSHOT)
24%mp_ds2csv(work.somedata,outfile="&_sasjs_work/test1.csv")
25%mp_assertscope(COMPARE)
26
27%let test1b=FAIL;
28data _null_;
29 infile "&_sasjs_work/test1.csv";
30 input;
31 list;
32 if _n_=1 then call symputx('test1a',_infile_);
33 else if _infile_=:'1," t""w""o",.Z' then call symputx('test1b','PASS');
34run;
35
36%mp_assert(
37 iftrue=("&test1a"="x factor, Y, Z"),
38 desc=Checking header row Test 1,
39 outds=work.test_results
40)
41%mp_assert(
42 iftrue=("&test1b"="PASS"),
43 desc=Checking data row Test 1,
44 outds=work.test_results
45)
46
47/**
48 * Test 2 - NAME header with fileref and semicolons
49 */
50filename test2 "&_sasjs_work/test2.csv";
51%mp_ds2csv(work.somedata,outref=test2,dlm=SEMICOLON,headerformat=NAME)
52
53%let test2b=FAIL;
54data _null_;
55 infile test2;
56 input;
57 list;
58 if _n_=1 then call symputx('test2a',_infile_);
59 else if _infile_=:'1;" t""w""o";.Z' then call symputx('test2b','PASS');
60run;
61
62%mp_assert(
63 iftrue=("&test2a"="X; Y; Z"),
64 desc=Checking header row Test 2,
65 outds=work.test_results
66)
67%mp_assert(
68 iftrue=("&test2b"="PASS"),
69 desc=Checking data row Test 2,
70 outds=work.test_results
71)
72
73/**
74 * Test 3 - SASjs format
75 */
76filename test3 "&_sasjs_work/test3.csv";
77%mp_ds2csv(work.somedata,outref=test3,headerformat=SASJS)
78
79%let test3b=FAIL;
80data _null_;
81 infile test3;
82 input;
83 list;
84 if _n_=1 then call symputx('test3a',_infile_);
85 else if _infile_=:'1," t""w""o",.Z' then call symputx('test3b','PASS');
86run;
87
88%mp_assert(
89 iftrue=("&test3a"="X:best32. Y:$char7. Z:best32."),
90 desc=Checking header row Test 3,
91 outds=work.test_results
92)
93%mp_assert(
94 iftrue=("&test3b"="PASS"),
95 desc=Checking data row Test 3,
96 outds=work.test_results
97)
98
99/* test 4 - sasjs with compare */
100data work.baseball ;
101attrib
102Name length= $18 label="Player's Name"
103Team length= $14 label="Team at the End of 1986"
104nAtBat length= 8 label="Times at Bat in 1986"
105nHits length= 8 label="Hits in 1986"
106nHome length= 8 label="Home Runs in 1986"
107nRuns length= 8 label="Runs in 1986"
108nRBI length= 8 label="RBIs in 1986"
109nBB length= 8 label="Walks in 1986"
110YrMajor length= 8 label="Years in the Major Leagues"
111CrAtBat length= 8 label="Career Times at Bat"
112CrHits length= 8 label="Career Hits"
113CrHome length= 8 label="Career Home Runs"
114CrRuns length= 8 label="Career Runs"
115CrRbi length= 8 label="Career RBIs"
116CrBB length= 8 label="Career Walks"
117League length= $8 label="League at the End of 1986"
118Division length= $8 label="Division at the End of 1986"
119Position length= $8 label="Position(s) in 1986"
120nOuts length= 8 label="Put Outs in 1986"
121nAssts length= 8 label="Assists in 1986"
122nError length= 8 label="Errors in 1986"
123Salary length= 8 label="1987 Salary in $ Thousands"
124Div length= $16 label="League and Division"
125logSalary length= 8 label="Log Salary"
126;
127infile cards dsd;
128input
129 Name :$char.
130 Team :$char.
131 nAtBat
132 nHits
133 nHome
134 nRuns
135 nRBI
136 nBB
137 YrMajor
138 CrAtBat
139 CrHits
140 CrHome
141 CrRuns
142 CrRbi
143 CrBB
144 League :$char.
145 Division :$char.
146 Position :$char.
147 nOuts
148 nAssts
149 nError
150 Salary
151 Div :$char.
152 logSalary
153;
154missing 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 _;
155/* fix precision issues */
156if not missing(logSalary) then logsalary=logSalary*1;
157datalines4;
158"Allanson, Andy",Cleveland,293,66,1,30,29,14,1,293,66,1,30,29,14,American,East,C,446,33,20,.,AE,.
159"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
160"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
161"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
162"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
163"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
164"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
165"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
166"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
167"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
168"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
169"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
170"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
171"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
172"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
173"Beane, Billy",Minneapolis,183,39,3,20,15,11,3,201,42,3,20,16,11,American,West,OF,118,0,0,.,AW,.
174"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
175"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
176"Bochte, Bruce",Oakland,407,104,6,57,43,65,12,5233,1478,100,643,658,653,American,West,1B,912,88,9,.,AW,.
177;;;;
178run;
179filename example temp lrecl=5000;
180%mp_ds2csv(work.baseball,outref=example,headerformat=SASJS)
181data _null_; infile example; input;put _infile_; if _n_>5 then stop;run;
182
183data _null_;
184 infile example;
185 input;
186 call symputx('stmnt',_infile_);
187 stop;
188run;
189data work.want;
190 infile example dsd firstobs=2;
191 input &stmnt;
192run;
193
194%mp_assert(
195 iftrue=(&syscc =0),
196 desc=Checking syscc prior to compare of work.baseball,
197 outds=work.test_results
198)
199
200proc compare base=want compare=work.baseball
201 method = absolute criterion = 0.0000000001 ;
202run;
203%mp_assert(
204 iftrue=(&sysinfo le 41),
205 desc=Checking compare of work.baseball,
206 outds=work.test_results
207)
208
209/* test 5 - sasjs with time/datetime/date */
210filename f2 temp;
211data work.test5;
212 do x=1 to 5;
213 y=x;
214 z=x;
215 end;
216 format x date9. y datetime19. z time.;
217run;
218%mp_ds2csv(work.test5,outref=f2,headerformat=SASJS)
219data _null_; infile example; input;put _infile_; if _n_>5 then stop;run;
220
221data _null_;
222 infile f2;
223 input;
224 putlog _infile_;
225 call symputx('stmnt2',_infile_);
226 stop;
227run;
228data work.want5;
229 infile f2 dsd firstobs=2;
230 input &stmnt2;
231 putlog _infile_;
232run;
233
234%mp_assert(
235 iftrue=(&syscc=0),
236 desc=Checking syscc prior to compare of test5,
237 outds=work.test_results
238)
239
240proc compare base=want5 compare=work.test5;
241run;
242%mp_assert(
243 iftrue=(&sysinfo le 41),
244 desc=Checking compare of work.test5,
245 outds=work.test_results
246)
247