Replace character In string
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 | * How to replace certain character(s) in a string. *Easy? Since SPSS appears to lack a native string replacement function, this requires a fair amount of brute force, especially if there are more than one occurences of the target character in your cases variable. *I came up with the following syntax, which has been successfully tested: *By Tim Dunsworth SPSS-L list April 10,2000. DO IF (INDEX(cases,"&")>0). LOOP. COMPUTE cases = CONCAT(SUBSTR(cases,1,INDEX(cases,"&")-1),"/", SUBSTR(cases,INDEX(cases,"&")+1,LENGTH(cases)-INDEX(cases,"&"))). END LOOP IF (INDEX(cases,"&")=0). END IF. EXECUTE. *------------------------------------------------------. *Next solution was suggested by Tverdek Edward from SPSS. *Reads in some data. DATA LIST /mystring 1-5 (A). BEGIN DATA &abcd e&gh& jk&mn o&q&s tuvw& END DATA. *Parses the original string variable. VECTOR x (5,A). LOOP #i=1 TO 5. COMPUTE x(#i)=SUBSTR(mystring,#i,1). END LOOP. *Recodes characters. RECODE x1 TO x5 ('&'='/'). *Recombines with substituted characters. STRING new (A5). COMPUTE new=CONCAT(x1,x2,x3,x4,x5). LIST. ************************************************. * this incomplete solution was suggested by Hillel Vardi on 2000/04/11 (need to add a loop). compute II=INDEX(cases,"&") DO IF II GT 0 COMPUTE SUBSTR(cases,II,1)="/" END IF |
Related pages
...