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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
*(Q) How can I produce the following table.

Simple Table Design...
 	PRODUCT	V.Poor	Poor	Neutral	Good	Exc.	Mean	N	 
	Product A	17%	8%	33%	17%	25%	3.25	12	 
	Product B	17%	8%	33%	17%	25%	3.25	12	 
	Product C	17%	8%	33%	8%	33%	3.33	12	 
	Product D	42%	8%	8%	8%	33%	2.83	12	 
	Product E	17%	42%	8%	8%	25%	2.83	12	 
	Product F	17%	17%	33%	8%	25%	3.08	12	 
	Product G	42%	8%	17%	8%	25%	2.67	12	 
	Product H	17%	0%	17%	42%	25%	3.58	12	 

*(A) Posted to SPSSX-L by Raynald Levesque on 2002/09/21.
* Visit http://pages.infinit.net/rlevesqu/index.htm

* The solutions below assume that there are less than approx 32,700 cases
 (The flipped file can have a maximum of 32,768 variables). If this
 limitation is a problem let me know and I will look for a workaround.


***********************.
* First method assumes we know the number of cases.
***********************.

DATA LIST LIST /ProdA ProdB ProdC ProdD ProdE ProdF ProdG ProdH.
BEGIN DATA
1 1 1 1  1 1 1 1 
2 2 2 2  3 3 3 3 
3 3 3 3  4 4 4 4 
1 1 1 1  2 2 3 3 
4 4 4 4  1 1 1 1
5 5 5 5  2 2 2 4
4 4 5 5  5 5 5 5
3 3 3 1  2 3 1 4
3 3 3 1  2 3 1 4
3 3 3 1  2 3 1 4
END DATA.

FLIP.

VECTOR v=var001 TO var010.
DO REPEAT cnt=1 TO 5 /c=c1 TO c5.
. COUNT c=var001 TO var010 (cnt).
. COMPUTE meanval=MEAN(var001 TO var010).
. COMPUTE n=NVALID(var001 TO var010).
. COMPUTE c=c/n*100.
. FORMATS c(PCT4.0).
END REPEAT PRINT.

STRING product(A14).
COMPUTE product = CONCAT("Product ",SUBSTR(case_lbl,5,1)).

FORMATS n(COMMA8).
VARIABLE LABEL meanval "Mean"
	c1 "Very Poor" /c2 "Poor" /c3 "Neutral" /c4 "Good" /c5 "Excellent".

SUMMARIZE
  /TABLES=product c1 c2 c3 c4 c5 meanval n
  /FORMAT=VALIDLIST NOCASENUM TOTAL
  /TITLE='Simple Table Design...'
  /MISSING=VARIABLE
  /CELLS=NONE.
EXECUTE.


***********************.
* Second method is completely general. The code determines the number
* of cases and adjust the syntax accordingly.
***********************.

DATA LIST LIST /ProdA ProdB ProdC ProdD ProdE ProdF ProdG ProdH.
BEGIN DATA
1 1 1 1  1 1 1 1 
2 2 2 2  3 3 3 3 
3 3 3 3  4 4 4 4 
1 1 1 1  2 2 3 3 
4 4 4 4  1 1 1 1
5 5 5 5  2 2 2 4
4 4 5 5  5 5 5 5
3 3 3 1  2 3 1 4
3 3 3 1  2 3 1 4
3 3 3 1  2 3 1 4
5 5 5 5  5 5 5 5
5 5 5 5  5 5 5 5
END DATA.

SAVE OUTFILE='c:\\temp\\original data.sav'.

* Calculate # of cases.

COMPUTE nobreak=1.
RANK VARIABLES=proda BY nobreak  /N INTO nbcases.
DO IF $CASENUM=1.
WRITE OUTFILE='c:\\temp\\nbcases.sps' /"DEFINE !nb()"nbcases"!ENDDEFINE.".
END IF.
EXECUTE.
INCLUDE 'c:\\temp\\nbcases.sps'.

* Define the variable names after the FLIP.
STRING v(A8).
COMPUTE v=CONCAT("v",LTRIM(STRING($CASENUM,F7))).
FLIP VARIABLES=proda TO prodh  /NEWNAME=v  .


*////////////////////.
DEFINE !doit().

!LET !vlast=!CONCAT(v,!nb)
VECTOR v=v1 TO !vlast.
DO REPEAT cnt=1 TO 5 /c=c1 TO c5.
. COUNT c=v1 TO !vlast (cnt).
. COMPUTE meanval=MEAN(v1 TO !vlast).
. COMPUTE n=NVALID(v1 TO !vlast).
. COMPUTE c=c/n*100.
. FORMATS c(PCT4.0).
END REPEAT PRINT.

STRING product(A14).
COMPUTE product = CONCAT("Product ",SUBSTR(case_lbl,5,1)).

FORMATS n(COMMA8).
VARIABLE LABEL meanval "Mean"
	c1 "Very Poor" /c2 "Poor" /c3 "Neutral" /c4 "Good" /c5 "Excellent".

SUMMARIZE
  /TABLES=product c1 c2 c3 c4 c5 meanval n
  /FORMAT=VALIDLIST NOCASENUM TOTAL
  /TITLE='Simple Table Design...'
  /MISSING=VARIABLE
  /CELLS=NONE.

!ENDDEFINE.
*////   /////  //////.

* Call macro to do it.
SET MPRINT=yes.
!doit.
SET MPRINT=no.