@Rakesh93
@hashman has given a solution. I improve my previous program to do the job dynamically. You have now a choice to choose.
As before text line is split into 20 characters with word-boundary. The split sub-list is saved as a big list each separated by a pipe character (|). The maximum number of such splits are passed as a macro variable for the next data step.
In that step, sub-lists are scanned as variables for output.
data have;
input txt $80.;
datalines;
I love hash tables. You will, too, after you see what they can do.
This paper will start with the classic 2 table join.
Nothing new, you say? But this version does it in one step without
sorting. And, it's the fastest method around.
Next, you will see the use of hash tables to join multiple
datasets on different keys in one pass, again with no
sorting. How about unlimited recursive chained lookups
(like when you need to update an ID through as many
incarnations as it may have had)? Hash tables solve that one,
too. And before you catch your breath, you'll see
how to create n datasets from one (or more), where n is
determined (at runtime) by the number of values
encountered in a variable. No muss, no fuss. One step
(well, OK, this one does require a prior sort) and you're
done. We’ll wind up with an outer join, just to show that hash
tables can do that, too. Take a look at what they can
deliver, and you'll be counting the ways you love hash tables
before the year is out.
;
run;
The program:
%let w = 20;
data temp;
set have end = eof;
length w $ &w;
length str $80 strout $80;
str = compbl(txt); /* remove more than one SPACE */
split = 0;
retain maxsplit;
do until(str=' ');
/* Check the Word boundary ends at the W-th position.
Confirm it based on the next position */
if char(str,&w) ne ' ' and char(str, &w+1)=' ' then i=&w+1;
else
do i = &w by - 1 until(s = ' ');
s = char(str,i);
end;
w = substr(str,1,i-1);
call catx('|', strout, w);
split + 1;
str = substr(str, i+1);
end;
maxsplit = max(maxsplit, split);
if eof then call symputx('maxsplit', maxsplit);
keep strout;
run;
data want;
set temp;
array s[&maxsplit] $ &w;
do i = 1 to &maxsplit;
s[i] = scan(strout,i,'|');
end;
drop i strout;
run;
... View more