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
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.