Convert first letter of each word to uppercase (v.2)
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 | * Encoding: UTF-8. * Convert 1st letter of each Word to Upper Case * Version 2, revision 14.09.2017 * Thanks to A. Paul Beaulne for sending me this code. * Also thanks to Joost Schouppe who suggested minor code correction to fix situations when no spaces found in address line. DATA LIST FIXED /address 1-50 (A). BEGIN DATA. 248 n. delphia ave. 9273 Fairvew dr. apt 410 6324 Meadowview corner of south & maine oneWord --- END DATA. * Define 5 new string variables to hold the "pieces" of the * address. You may need to define more if you know there are * more separate words in the address. STRING a1 TO a5 (A50). * Use the DO REPEAT command to repeat the process for each * "piece" of the address (each separate word, number, etc.). * First find the location of the first space in the address. * Then compute a substring which is the address variable up * until the first space. This is stand-in variable a, which * represents a "piece" of the address. * Then shorten the address variable by making it a substring * of itself from the first space until the end. In other words, * the address variable has the new "piece" trimmed from the * front of it. * Use the CONCAT and UPCASE functions to convert the first * character of the new "piece" you've created to upper case & * concatenate it back together with the remainder of the new * piece (the stand-in variable a). * This sequence loops through 5 times. If you know you'll have * more than 5 "pieces" in the address, define more than 5 * variables, i.e., define strings a1 to a8, instead. DO REPEAT a=a1 TO a5. COMPUTE space=CHAR.INDEX(address," "). IF space=0 space=LENGTH(address)+1. /* this avoides swalloing last/single word in a line, when no space found */ COMPUTE a=SUBSTR(address,1,space-1). COMPUTE address=SUBSTR(address,space+1). COMPUTE a=CONCAT(UPCASE(SUBSTR(a,1,1)),SUBSTR(a,2)). END REPEAT PRINT. * Now concatenate the pieces together into the new address * variable. Use the RTRIM function to trim trailing blanks, and * insert single blanks where appropriate. COMPUTE address=CONCAT(RTRIM(a1)," ", RTRIM(a2)," ",RTRIM(a3)," ", RTRIM(a4)," ",RTRIM(a5)). LIST variables=address. |
This is revised version, see original syntax here: Convert first letter of each word to uppercase.
You may want also to check (more flexible) Python-based solution: Capitalize each word in a string variable