Automatically infer variables' measurement level from format and data.
[pspp] / tests / language / stats / autorecode.at
1 dnl PSPP - a program for statistical analysis.
2 dnl Copyright (C) 2017 Free Software Foundation, Inc.
3 dnl
4 dnl This program is free software: you can redistribute it and/or modify
5 dnl it under the terms of the GNU General Public License as published by
6 dnl the Free Software Foundation, either version 3 of the License, or
7 dnl (at your option) any later version.
8 dnl
9 dnl This program is distributed in the hope that it will be useful,
10 dnl but WITHOUT ANY WARRANTY; without even the implied warranty of
11 dnl MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 dnl GNU General Public License for more details.
13 dnl
14 dnl You should have received a copy of the GNU General Public License
15 dnl along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 dnl
17 AT_BANNER([AUTORECODE procedure])
18
19 AT_SETUP([AUTORECODE multiple missing values])
20 AT_DATA([autorecode.sps],
21   [DATA LIST LIST NOTABLE /u v w x y z (F2.0).
22 BEGIN DATA.
23 11 11 11 11 11 11
24 12 12 12 12 12 12
25 13 13 13 13 13 13
26 14 14 14 14 14 14
27 15 15 15 15 15 15
28 16 16 16 16 16 16
29 END DATA.
30
31 MISSING VALUES u (11)
32                v (11, 12)
33                w (11, 12, 13)
34                x (11 THRU 14)
35                y (11 THRU 15)
36                z (11 THRU 16).
37
38 AUTORECODE u v w x y z INTO a b c d e f/print.
39 LIST.
40 DISPLAY VARIABLES/VARIABLES=a TO f.
41 ])
42 AT_CHECK([pspp -O format=csv autorecode.sps], [0], [dnl
43 Table: Recoding u into a.
44 Old Value,New Value,Value Label
45 12,1,12
46 13,2,13
47 14,3,14
48 15,4,15
49 16,5,16
50 11,6,11
51
52 Table: Recoding v into b.
53 Old Value,New Value,Value Label
54 13,1,13
55 14,2,14
56 15,3,15
57 16,4,16
58 11,5,11
59 12,6,12
60
61 Table: Recoding w into c.
62 Old Value,New Value,Value Label
63 14,1,14
64 15,2,15
65 16,3,16
66 11,4,11
67 12,5,12
68 13,6,13
69
70 Table: Recoding x into d.
71 Old Value,New Value,Value Label
72 15,1,15
73 16,2,16
74 11,3,11
75 12,4,12
76 13,5,13
77 14,6,14
78
79 Table: Recoding y into e.
80 Old Value,New Value,Value Label
81 16,1,16
82 11,2,11
83 12,3,12
84 13,4,13
85 14,5,14
86 15,6,15
87
88 Table: Recoding z into f.
89 Old Value,New Value,Value Label
90 11,1,11
91 12,2,12
92 13,3,13
93 14,4,14
94 15,5,15
95 16,6,16
96
97 Table: Data List
98 u,v,w,x,y,z,a,b,c,d,e,f
99 11,11,11,11,11,11,6,5,4,3,2,1
100 12,12,12,12,12,12,1,6,5,4,3,2
101 13,13,13,13,13,13,2,1,6,5,4,3
102 14,14,14,14,14,14,3,2,1,6,5,4
103 15,15,15,15,15,15,4,3,2,1,6,5
104 16,16,16,16,16,16,5,4,3,2,1,6
105
106 Table: Variables
107 Name,Position,Print Format,Write Format,Missing Values
108 a,7,F1.0,F1.0,6
109 b,8,F1.0,F1.0,5; 6
110 c,9,F1.0,F1.0,4; 5; 6
111 d,10,F1.0,F1.0,3 THRU 6
112 e,11,F1.0,F1.0,2 THRU 6
113 f,12,F1.0,F1.0,1 THRU 6
114 ])
115 AT_CLEANUP
116
117 AT_SETUP([AUTORECODE numbers and short strings])
118 AT_DATA([autorecode.sps],
119   [data list /X 1-5(a) Y 7.
120 begin data.
121 lasdj 1
122 asdfk 0
123 asdfj 2
124 asdfj 1
125 asdfk 2
126 asdfj 9
127 lajks 9
128 asdfk 0
129 asdfk 1
130 end data.
131
132 missing values x('asdfk') y(9).
133
134 autorecode x y into A B/descend/print.
135
136 list.
137 compute Z=trunc(y/2).
138 formats z(F1.0).
139 autorecode z into W.
140 list.
141 ])
142 AT_CHECK([pspp -O format=csv autorecode.sps], [0],
143   [Table: Reading 1 record from INLINE.
144 Variable,Record,Columns,Format
145 X,1,1-5,A5
146 Y,1,7-7,F1.0
147
148 Table: Recoding X into A.
149 Old Value,New Value,Value Label
150 lasdj,1,lasdj
151 lajks,2,lajks
152 asdfj,3,asdfj
153 asdfk,4,asdfk
154
155 Table: Recoding Y into B.
156 Old Value,New Value,Value Label
157 2,1,2
158 1,2,1
159 0,3,0
160 9,4,9
161
162 Table: Data List
163 X,Y,A,B
164 lasdj,1,1,2
165 asdfk,0,4,3
166 asdfj,2,3,1
167 asdfj,1,3,2
168 asdfk,2,4,1
169 asdfj,9,3,4
170 lajks,9,2,4
171 asdfk,0,4,3
172 asdfk,1,4,2
173
174 Table: Data List
175 X,Y,A,B,Z,W
176 lasdj,1,1,2,0,1
177 asdfk,0,4,3,0,1
178 asdfj,2,3,1,1,2
179 asdfj,1,3,2,0,1
180 asdfk,2,4,1,1,2
181 asdfj,9,3,4,.,.
182 lajks,9,2,4,.,.
183 asdfk,0,4,3,0,1
184 asdfk,1,4,2,0,1
185 ])
186 AT_CLEANUP
187
188 AT_SETUP([AUTORECODE long strings and check the value labels])
189 AT_DATA([ar.sps],
190   [data list notable list /s (a16) x (f1.0).
191 begin data.
192 widgets      1
193 thingummies  2
194 oojars       3
195 widgets      4
196 oojars       5
197 thingummies  6
198 oojimiflips  7
199 end data.
200
201 variable labels s 'tracking my stuff'.
202 value labels /s 'thingummies' 'Funny sticky things'.
203
204 autorecode s into new/print.
205
206 list.
207
208 display dictionary/variables=new.
209 ])
210
211 AT_CHECK([pspp -O format=csv ar.sps], [0],
212   [Table: Recoding s into new (tracking my stuff).
213 Old Value,New Value,Value Label
214 oojars,1,oojars
215 oojimiflips,2,oojimiflips
216 thingummies,3,Funny sticky things
217 widgets,4,widgets
218
219 Table: Data List
220 s,x,new
221 widgets,1,4
222 thingummies,2,3
223 oojars,3,1
224 widgets,4,4
225 oojars,5,1
226 thingummies,6,3
227 oojimiflips,7,2
228
229 Table: Variables
230 Name,Position,Label,Measurement Level,Role,Width,Alignment,Print Format,Write Format
231 new,3,tracking my stuff,Nominal,Input,8,Right,F1.0,F1.0
232
233 Table: Value Labels
234 Variable Value,,Label
235 tracking my stuff,1,oojars
236 ,2,oojimiflips
237 ,3,Funny sticky things
238 ,4,widgets
239 ])
240
241 AT_CLEANUP
242
243
244 AT_SETUP([AUTORECODE group subcommand])
245 AT_DATA([ar-group.sps],
246 [data list notable list /x y (f8.0).
247 begin data.
248 11 10
249 12 12
250 13 15
251 14 11
252 15 12
253 16 18
254 end data.
255
256 missing values y (12).
257
258 autorecode
259         x y into a b
260         /group
261         /print.
262
263 list.
264 display variables /variables=a b.
265 ])
266
267 AT_CHECK([pspp -O format=csv ar-group.sps], [0],
268 [Table: Recoding grouped variables.
269 Old Value,New Value,Value Label
270 10,1,10
271 11,2,11
272 13,3,13
273 14,4,14
274 15,5,15
275 16,6,16
276 18,7,18
277 12,8,12
278
279 Table: Data List
280 x,y,a,b
281 11,10,2,1
282 12,12,8,8
283 13,15,3,5
284 14,11,4,2
285 15,12,5,8
286 16,18,6,7
287
288 Table: Variables
289 Name,Position,Print Format,Write Format,Missing Values
290 a,3,F1.0,F1.0,8
291 b,4,F1.0,F1.0,8
292 ])
293
294 AT_CLEANUP
295
296
297
298 AT_SETUP([AUTORECODE group - string variables])
299 AT_DATA([strings.sps],
300 [data list notable list /x (a8) y (a16).
301 begin data.
302 fred bert
303 charlie "         "
304 delta echo
305 "      " windows
306 " "  nothing
307 end data.
308
309
310 autorecode x y into a b
311         /group
312         /print.
313
314 delete variables x y.
315
316 list.
317
318 ])
319
320 AT_CHECK([pspp -O format=csv strings.sps], [0],
321 [Table: Recoding grouped variables.
322 Old Value,New Value,Value Label
323 ,1,
324 bert,2,bert
325 charlie,3,charlie
326 delta,4,delta
327 echo,5,echo
328 fred,6,fred
329 nothing,7,nothing
330 windows,8,windows
331
332 Table: Data List
333 a,b
334 6,2
335 3,1
336 4,5
337 1,8
338 1,7
339 ])
340
341 AT_CLEANUP
342
343
344 dnl Tests for a crash which happened when the /GROUP subcommand
345 dnl appeared with string variables of different widths.
346 AT_SETUP([AUTORECODE group vs. strings])
347 AT_DATA([ar-strings.sps],
348   [data list notable list /a (a12) b (a6).
349 begin data.
350 one    nine
351 two    ten
352 three  eleven
353 four   nought
354 end data.
355
356 autorecode a b into x y
357         /group
358         /print.
359
360 list.
361 ])
362
363 AT_CHECK([pspp -O format=csv ar-strings.sps], [0], [dnl
364 Table: Recoding grouped variables.
365 Old Value,New Value,Value Label
366 eleven,1,eleven
367 four,2,four
368 nine,3,nine
369 nought,4,nought
370 one,5,one
371 ten,6,ten
372 three,7,three
373 two,8,two
374
375 Table: Data List
376 a,b,x,y
377 one,nine,5,3
378 two,ten,8,6
379 three,eleven,7,1
380 four,nought,2,4
381 ])
382
383 AT_CLEANUP
384
385
386
387 AT_SETUP([AUTORECODE /blank])
388
389 AT_DATA([auto-blank.sps],  [dnl
390 data list notable list /x (a8) y (f8.0) z (a16).
391 begin data.
392 one   2  fred
393 two   4  ""
394 ""    4  fred
395 ""    2  charliebrown
396 three 2  charliebrown
397 end data.
398
399 autorecode variables x y z into a b c  /blank=missing /print.
400
401 list a b c y.
402 ])
403
404 AT_CHECK([pspp -O format=csv auto-blank.sps], [0], [dnl
405 Table: Recoding x into a.
406 Old Value,New Value,Value Label
407 one,1,one
408 three,2,three
409 two,3,two
410
411 Table: Recoding y into b.
412 Old Value,New Value,Value Label
413 2,1,2
414 4,2,4
415
416 Table: Recoding z into c.
417 Old Value,New Value,Value Label
418 charliebrown,1,charliebrown
419 fred,2,fred
420
421 Table: Data List
422 a,b,c,y
423 1,1,2,2
424 3,2,.,4
425 .,2,2,4
426 .,1,1,2
427 2,1,1,2
428 ])
429
430 AT_CLEANUP
431
432 dnl AUTORECODE had a use-after-free error when TEMPORARY was in use.
433 dnl Bug #32757.
434 AT_SETUP([AUTORECODE with TEMPORARY])
435 AT_DATA([autorecode.sps],
436   [data list /X 1-5(a) Y 7.
437 begin data.
438 lasdj 1
439 asdfk 0
440 asdfj 2
441 asdfj 1
442 asdfk 2
443 asdfj 9
444 lajks 9
445 asdfk 0
446 asdfk 1
447 end data.
448
449 temporary.
450 select if y > 1.
451 autorecode x y into A B/descend/print.
452 list.
453 ])
454 AT_CHECK([pspp -O format=csv autorecode.sps], [0], [dnl
455 Table: Reading 1 record from INLINE.
456 Variable,Record,Columns,Format
457 X,1,1-5,A5
458 Y,1,7-7,F1.0
459
460 Table: Recoding X into A.
461 Old Value,New Value,Value Label
462 lajks,1,lajks
463 asdfk,2,asdfk
464 asdfj,3,asdfj
465
466 Table: Recoding Y into B.
467 Old Value,New Value,Value Label
468 9,1,9
469 2,2,2
470
471 Table: Data List
472 X,Y,A,B
473 lasdj,1,.,.
474 asdfk,0,2,.
475 asdfj,2,3,2
476 asdfj,1,3,.
477 asdfk,2,2,2
478 asdfj,9,3,1
479 lajks,9,1,1
480 asdfk,0,2,.
481 asdfk,1,2,.
482 ])
483 AT_CLEANUP
484
485
486 dnl For compatibility, make sure that /INTO (with leading slash) is accepted
487 dnl (bug #48762)
488 AT_SETUP([AUTORECODE with /INTO])
489 AT_DATA([autorecode.sps],
490   [data list list notable /x (f8.0).
491 begin data.
492 1
493 8
494 -901
495 4
496 1
497 99
498 8
499 end data.
500
501 autorecode x  /into y /print.
502
503 list.
504 ])
505 AT_CHECK([pspp -O format=csv autorecode.sps], [0],
506 [Table: Recoding x into y.
507 Old Value,New Value,Value Label
508 -901,1,-901
509 1,2,1
510 4,3,4
511 8,4,8
512 99,5,99
513
514 Table: Data List
515 x,y
516 1,2
517 8,4
518 -901,1
519 4,3
520 1,2
521 99,5
522 8,4
523 ])
524 AT_CLEANUP
525
526
527 AT_SETUP([AUTORECODE with /BLANK without specifier])
528
529 AT_DATA([autorecode.sps], [data list notable list /x (a18).
530 begin data
531 one
532 two
533 three
534 end data.
535
536 * /BLANK should be either =MISSING or =VALID
537 autorecode x /into y
538  /blank
539
540 execute.
541 ])
542
543 AT_CHECK([pspp -O format=csv autorecode.sps], [1], [ignore])
544
545 AT_CLEANUP