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

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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