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
140
141
142
143
144
* This macro is VERY useful (IMHO).
* To group values and define value labels for the grouped values.
* for instance define labels for age 1="0-4" 2="5-9" 3="10-14" ... 19="90-95".
* http://pages.infinit.net/rlevesqu/index.htm.

SET MPRINT=no.
*///////////////////////////////////////////////////////.
DEFINE !label (vname=!TOKENS(1)
                /vcoded=!TOKENS(1)
                /begr=!TOKENS(1)
                /endr=!TOKENS(1)
	    /diff=!TOKENS(1)
                /nbbins=!TOKENS(1))
/* Notes.
* Load data file then call this macro.
* vname 	=name of variable to be recoded.
* vcoded        	=name of variable which is to contain recoded value.
* begr 	 	=BEGinning of first Range.
* endr  		=ENDing of first Range (4 means 4.9999999...).
* diff		=display the endr as endr + 1 - diff.
* nbbins        	=NumBer of BINS.
* nbbins. begr and endr must be integers.
* Raynald Levesque rlevesque@videotron.ca

SAVE OUTFILE='C:\\temp\\data temp.sav'.
NEW file.
INPUT PROGRAM.
LOOP id=1 TO !nbbins.
+COMPUTE #delta=1+ !endr - !begr.
+DO IF id=1.
++COMPUTE begv=!begr.
++COMPUTE endv=begv + #delta.
++ELSE.
++COMPUTE begv=LAG(begv) + #delta.
++COMPUTE endv=LAG(endv) + #delta.
+END IF.
+END CASE.
END LOOP.
END FILE.
END INPUT PROGRAM.

STRING vname(A8).
COMPUTE vname=!QUOTE(!EVAL(!vname)).
COMPUTE dummy=0.
MATCH FILES FILE=* /BY dummy /LAST=last.
STRING vcoded(A8).
COMPUTE vcoded=!QUOTE(!EVAL(!vcoded)).
FORMATS id begv endv (F8.0).

* write syntax to recode the values.
DO IF $CASENUM=1.
WRITE OUTFILE="c:\\temp\\recode values1.sps"/"RECODE " vname " (" begv " THRU" endv " = " id " )".
ELSE IF not last.
WRITE OUTFILE="c:\\temp\\recode values1.sps"/" (" begv " THRU " endv " = " id" )".
END IF.
DO IF last.
WRITE OUTFILE="c:\\temp\\recode values1.sps"/" (" begv " THRU HIGHEST = " id" )".
WRITE OUTFILE="c:\\temp\\recode values1.sps"/" (MISSING = 999999)".
WRITE OUTFILE="c:\\temp\\recode values1.sps"/" INTO " vcoded  ". ".
END IF.

* write syntax to define the value labels.
COMPUTE endv=RND(endv).
STRING q1(A1) /label1(A14).
COMPUTE q1="'".
DO IF not last.
COMPUTE label1=CONCAT(LTRIM(STRING(begv,F8.0))," -",LTRIM(STRING(endv-!diff,F8.2))).
ELSE.
COMPUTE label1=CONCAT(LTRIM(STRING(begv,F8.0))," thru hi").
END IF.
DO IF $CASENUM=1.
WRITE OUTFILE="c:\\temp\\recode values2.sps"
        /"ADD VALUE LABELS " vcoded " 999999 " q1 "Missing" q1 ".".
END IF.
WRITE OUTFILE="c:\\temp\\recode values2.sps"
        /"ADD VALUE LABELS " vcoded  id " " q1 label1 q1 ".".

EXECUTE.
GET FILE='C:\\temp\\data temp.sav'.
INCLUDE "c:\\temp\\recode values1.sps".
INCLUDE "c:\\temp\\recode values2.sps".
SET TNUMBERS=LABELS /TVARS=LABELS.
VARIABLE LABEL !vcoded !QUOTE(!vname).
EXECUTE.

!ENDDEFINE.
*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\.


DATA LIST LIST /sales.
BEGIN DATA
.
.
.00
.00
.00
.00
10.99
14.80
27.99
29.80
29.90
31.99
31.99
32.95
32.99
45.99
54.95
80.50
81.80
103.59
115.95
120.99
141.50
309.60
374.90
END DATA.
LIST.

**************************.
* Sample use of macro.
**************************.
SET MPRINT=yes.
!label vname=sales vcoded=c_sales begr=0 endr=24 diff=.01 nbbins=10.

FREQ c_sales.

* Example 2.
* Create data file to illustrate the use of the macro. 
DATA LIST FREE /age. 
BEGIN DATA 
29.99 23 24.99 25 25.01 2 5 27 65 48.3 43 90.2 71 52 35 74 
END DATA. 
LIST /CASES FROM 1 TO 2. 
COMPUTE service=MAX(age-15+UNIFORM(10),0). 

**************************. 
* Sample use of macro. 
**************************
!label vname=age vcoded=c_age begr=0 endr=4 diff=.01 nbbins=21. 
!label vname=service vcoded=c_serv begr=0 endr=9 diff=.01 nbbins=10. 
FREQ c_age c_serv. 
CROSSTABS /TABLES=c_age BY c_serv 
	/FORMAT= AVALUE TABLES /CELLS= COUNT .