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
* Two string variables (problem and race) contain a variable number of comma separated numbers. How can I parse this info?.

* Answer posted to usenet by rlevesque@videotron.ca.

* Sample data file.
DATA LIST LIST /problem(A12) race(A12).
BEGIN DATA
'1,2' 	'4,5'
'10,11,3,15'	'5'
END DATA.
LIST.

* Define a macro to do the job.
DEFINE !parse (var=!TOKENS(1) /nbval=!TOKENS(1))
COMPUTE !var=CONCAT(RTRIM(!var),',').
STRING #str(A8).
VECTOR !var (!nbval F8.0).
COMPUTE #beg=1.
LOOP #cnt=1 TO !nbval.
+COMPUTE #str=SUBSTR(!var,#beg).
+COMPUTE #end=INDEX(#str,',')-1.
+DO IF #end=-1.
+ BREAK.
+END IF.
+COMPUTE !var(#cnt)=NUMBER(SUBSTR(#str,1,#end),F8.0).
+COMPUTE #beg=#beg+#end+1.
END LOOP IF #end=-1.
EXECUTE.
!ENDDEFINE.

* Now call the macro.
!parse var=problem nbval=5.
!parse var=race nbval=4.