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
*(Q) I have a variable containing integers of 3 or more digits.
* I need to extract the first digit if the integer has 3 digits and the
* first 2 digits otherwise.

*(A) posted to SPSSX-L list by Raynald Levesque on 2003/11/20.

* Data example.
DATA LIST LIST /asoc.
BEGIN DATA
813
1314
99999
888888
7777777
45321547
9999999999
END DATA.
VARIABLE WIDTH asoc(10).

* Solution.
DO IF asoc<10000.
- COMPUTE asoc2=TRUNC(asoc/100).
ELSE.
- COMPUTE #digits=TRUNC(LG10(asoc)).
- COMPUTE asoc2=TRUNC(asoc/(10**(#digits-1))).
END IF.
EXECUTE.