* 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.