1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
* Automated data transform from tall to wide.

* Question: I have data such as given in the DATA LIST below and need to create 1 case per value of var1.
* Since the maximum number of values of a given variable var1 will vary each time the syntax is run, 
* I would like to have a syntax which adapts itself to different data files.

* Answer: posted to newsgroup by rlevesque@videotron.ca on 2001/06/23.

DATA LIST LIST /var1(A1) var2(F8.0).
BEGIN DATA
A    1
A    7
A    32
A    5
B    9
C    34
C    9
D    3
D    7
D    0
D    1
END DATA.
LIST.
AUTORECODE VARIABLES=var1 /INTO var1num.
SAVE OUTFILE='c:\\temp\\mydata.sav'.

** Define a macro which will contain the number of required columns **.
SET MPRINT=no.

AGGREGATE
  /OUTFILE=*
  /BREAK=var1num
  /n = N(var1num).

COMPUTE nobreak=1.
AGGREGATE
  /OUTFILE=*
  /BREAK=nobreak
  /n = MAX(n).

DO IF $CASENUM=1.
WRITE OUTFILE='c:\\temp\\define n.sps' /'DEFINE !n()'n'!ENDDEFINE.'.
END IF.
EXECUTE.
INCLUDE FILE='c:\\temp\\define n.sps'.

** Now define a macro to do the job **.

*/////////////////////////.
DEFINE !doit(!POS=!TOKENS(1))

COMPUTE casen=$CASENUM.
RANK
  VARIABLES=casen  (A) BY !1  /RANK /PRINT=NO.
VECTOR vec(!n F8.0).
COMPUTE vec(rcasen)=var2.
AGGREGATE
  /OUTFILE=*
  /BREAK=var1
  /vec1 TO !CONCAT(vec,!n) = MAX(vec1 TO !CONCAT(vec,!n)).
!ENDDEFINE.
*/////////////////////////.

GET FILE='c:\\temp\\mydata.sav'.

SET MPRINT=yes.
!doit var1num.