BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Jedrzej
Obsidian | Level 7

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)

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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

 

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Is a´defunct 1 or 2 words in this case?

Jedrzej
Obsidian | Level 7
one, sorry mistype
PeterClemmensen
Tourmaline | Level 20

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

 

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1126 views
  • 0 likes
  • 2 in conversation