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
* This macro is useful when you have to change the format from 
* string to string (ss)
* numeric to string (ns) or
* string to numeric (sn).
* while retaining the original variable name.

* rlevesque@videotron.ca 2001/05/23.

*/////////////////////.
DEFINE !convert (vname=!TOKENS(1)
               /nformat=!TOKENS(1)
               /type=!TOKENS(1))

/* where vname= the name of the variable to change format */
/* nformat = the new format */
/* type of change ss (string to string) ns or sn where n=numeric and s=string */

!IF (!type='ss') !THEN
STRING temp1234(!nformat).
COMPUTE temp1234=!vname.
!IFEND

!IF (!type='ns') !THEN
STRING temp1234(!nformat).
COMPUTE temp1234=LTRIM(STRING(!vname,F18.0)).
!IFEND

!IF (!type='sn') !THEN
COMPUTE temp1234=NUMBER(!vname,F18.0).
FORMAT temp1234(!nformat).
!IFEND

MATCH FILES FILE=* /DROP=!vname.
RENAME VARIABLE (temp1234=!vname).
!ENDDEFINE.
*/////////////////////.


* EXAMPLE of how the macro is used.

DATA LIST LIST /var1(A8), var2(A8), var3(F8.0).
BEGIN DATA
abcdefg	254	235
adadad	128	265
END DATA.
LIST.

!convert vname=var1  nformat=A5		type=ss.
*The above command converts variable var1 form an A8 to an A5 string.

!convert vname=var2  nformat=F8.2		type=sn.
*The above command converts variable var2 form an A8 to an F8.2 number.
* (Of course if contains an invalid number, the resulting numeric variable will be missing.

!convert vname=var3  nformat=A8		type=ns.
*The above command converts numeric var3 form an F8.2 to an A8 string.