@knveraraju91
Regardless of the issue of a blank in position 200 vs 201, it appears you're doing a lot of work for a straightforward task.
Your strategy is to count words, and sum up their cumulative lengths (plus a blank per word) to know when words will be assigned to a new group. Then, for each group, you concatenate those words into the new OUTPUT variable.
Consider this strategy:
Use the COMPBL function to remove excess internal blanks (i.e. double blanks become single blanks) in VALUE.
Instead of counting words, why not start with a txt length (variable TXTLEN) of 200, beginning with character 1 (variable STARTCHR=1) and ending with character 200 and see if it is immediately followed by a blank (i.e. look at column 201=STARTCHR+TXTLEN for a blank). If it's not a blank, reduce txt length by 1 and see if it is followed by a blank. Once a blank has been found copy all the text from STARTCHR and length TXTLEN to value1. Then for value2, increment STARTCHR by TXTLEN+1 (i.e. point at first unproceessed non-blank), look for blanks at position STARTCHR+200 and work backward until one is found. Copy the text from the new STARTCHR for a length of the new TXTLEN to value2. Etc. Etc. If startchr finally get incremented beyond length of value, then no further work is needed - stop the loop.
This is a lot less programming than counting words and concatenating them 1 at a time.
regards,
Mark That strategy is in the loop below:
data want (drop=v startchr txtlen);
array val{4} $200 value1-value4;
set data;
n+1;
value=compbl(value); *remove excess blanks *;
startchr=1;
do v=1 to dim(val) until (startchr>length(value));
/* Nothing inside this do loop since we only want it to stop when a blank is found*/
do txtlen=200 to 1 by -1 while(notspace(char(value,startchr+txtlen)));
end;
/* copy from startchr for length txtlen to value1, value2, value3, value4*/
val{v}=substr(value,startchr,txtlen);
startchr=startchr+txtlen+1;
end;
run;
... View more