/* Richard A. DeVenezia * www.devenezia.com */ /* This was originally posted to SAS-L on Oct. 24, 2003 * in response to a post with * Subject: Parsing child strings from a parent string */ /* This sample breaks up a long string into * N smaller character variables split1-splitN. * Each split{i} variable has the same length, L. * The breaking up follows these rules: * - as many whole words as fit in each split{i} * - if a word is longer than split{i}, it gets hyphenated by position * (not by any lexical smarts) * - if N*L is too short to hold the entire original string * then split{N} is an ellipsis (...) to indicate there is more * (which got lost) */ %* %* A DATA set with a long string %*; data test; info = 'SUBJECT IS WILLING TO HOLD SUN EXPOSURE REASONABLY CONSTANT AND TO AVOID USE OF TANNING BOOTHS OR OTHER UV LIGHT SOURCES BEGINNING 28 DAYS BEFORE EFALIZUMAB DOSING AND CONTINUING THROUGHOUT THE STUDY.'; output; run; %* %* Specify L and N %*; %let splitWidth=9; %* Shorter than typical to demonstrate hyphenation; %let splitCount=25; %* Smaller than typical to demonstrate ellipsis; %* %* Split the long string %*; data splitest; set test; length split1-split&splitCount $&splitWidth; array split split1-split&splitCount; i = 0; p0 = addr (info); pE = p0 + length (info); do until (p0 >= pE or i >= dim(split)); p1 = min (p0 + &splitWidth-1, pE-1); do while (peekc(p1,1) ne ' ' and p1 > p0 and p1+1 ne pE); p1 + (-1); end; i+1; if peekc(p1,1) = ' ' or p1+1 eq pE then do; split [i] = peekc (p0, p1-p0+1); end; else do; p1 = p0 + &splitWidth-2; split [i] = peekc (p0, p1-p0+1) || '-'; end; if i=dim(split) and pE > p1 then split[i] = '...'; p0 = p1 + 1; end; keep split1-split&splitCount; run;