Hi,
I would like to split my variable in a way that if there are more words then 10 it would extract all words>10 into a new variable and the old one will have only ten words.
example
data have;
infile cards truncover;
input str $char150.;
cards;
New Year Copenhagen is the Capital of Denmark Jensen is a´defunct British manufacturer of sports cars Another line Paris is a capital of France
;
run;
and the output should be
str(New Year Copenhagen is the Capital of Denmark Jensen is)
str1(is a´defunct British manufacturer of sports cars Another line)
str2(Paris is a capital of France)
Ok. How about
data have;
infile cards truncover;
input str $char150.;
cards;
New Year Copenhagen is the Capital of Denmark Jensen is a´defunct British manufacturer of sports cars Another line Paris is a capital of France
;
run;
data temp(keep = g s);
set have;
length s $200;
do i = 1 to countw(str);
s = catx(' ', s, scan(str, i));
if countw(s)= 10 | i = countw(str) then do;
g + 1;
output;
s = '';
end;
end;
run;
proc transpose data = temp out = want(drop = _:) prefix = str;
id g;
var s;
run;
Result:
str1 str2 str3 New Year Copenhagen... is a´defunct... is a capital of France
Is a´defunct 1 or 2 words in this case?
Ok. How about
data have;
infile cards truncover;
input str $char150.;
cards;
New Year Copenhagen is the Capital of Denmark Jensen is a´defunct British manufacturer of sports cars Another line Paris is a capital of France
;
run;
data temp(keep = g s);
set have;
length s $200;
do i = 1 to countw(str);
s = catx(' ', s, scan(str, i));
if countw(s)= 10 | i = countw(str) then do;
g + 1;
output;
s = '';
end;
end;
run;
proc transpose data = temp out = want(drop = _:) prefix = str;
id g;
var s;
run;
Result:
str1 str2 str3 New Year Copenhagen... is a´defunct... is a capital of France
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.