Split variable >200 char into fragments <200 char but keep words whole

Accepted Solution Solved
Reply
Contributor
Posts: 26
Accepted Solution

Split variable >200 char into fragments <200 char but keep words whole

I am trying to split a variable that contains some values which are more than 200 char into two variables that contain only values less than 200 char without cutting any word in half.


Accepted Solutions
Solution
‎09-08-2016 02:27 PM
Respected Advisor
Posts: 4,995

Re: Split variable >200 char into fragments <200 char but keep words whole

Just because I've seen medical dictionaries with several words longer than 40 characters apiece, I would modify the ELSE DO section:

 

else do i=201 to 1 by -1;

   if substr(x, i, 1) = ' ' then do;

      string1 = substr(x, 1, i-1);

      string2 = left(substr(x, i+1));

      i=0;

   end;

end;

 

View solution in original post


All Replies
Grand Advisor
Posts: 10,224

Re: Split variable >200 char into fragments <200 char but keep words whole

How much longer than 200 characters is the source?

This may give you an idea. This assumes that you want to break on spaces only, add comma,period or other possible break characters to the second parameter of FINDC. This also assumes that you aren't likely to have any single word longer than 20 characters.

data example;
   x= "This is a really long string that I want to split into two strings of less than 200 characters. The current length is going to be a randome collection of words untill I get something long enough to split. Of course we may have some issues with how the long the longest individula word may be in the source string but that's life.";
   length string1 string2 $ 200;
   if length(x) < 200 then String1=X;
   Else do;
      string1 = substr(x,1,findc(x,' ',180));
      string2 = substr(x,findc(x,' ',180));
   end;
run;
Solution
‎09-08-2016 02:27 PM
Respected Advisor
Posts: 4,995

Re: Split variable >200 char into fragments <200 char but keep words whole

Just because I've seen medical dictionaries with several words longer than 40 characters apiece, I would modify the ELSE DO section:

 

else do i=201 to 1 by -1;

   if substr(x, i, 1) = ' ' then do;

      string1 = substr(x, 1, i-1);

      string2 = left(substr(x, i+1));

      i=0;

   end;

end;

 

Contributor
Posts: 26

Re: Split variable >200 char into fragments <200 char but keep words whole

Brilliant. I actually need this to make medical chart verbatim fit into SAS V5 transport files which have this $200. restriction. Thanks.     

Respected Advisor
Posts: 3,775

Re: Split variable >200 char into fragments <200 char but keep words whole

This looks like the easiest way to find the cut point.

 

cut = findc(x,' ',-200);

But I don't see why you need to bother won't the consumer of your XPORT file just glue the cut up text back together in one variable.  You could even supply the program.

 

Super Contributor
Posts: 270

Re: Split variable >200 char into fragments <200 char but keep words whole

Thank you very much. It is working for me. The variables are of different lengths. How to find exactly number of characters(length) in a variable value. Do you have any suggestions. Thank you.

 

 

Respected Advisor
Posts: 4,995

Re: Split variable >200 char into fragments <200 char but keep words whole

The LENGTHN function will do that:

 

n_characters = lengthn(varname);

 

Similarly, there is a LENGTH function.  It is almost identical, but returns a 1 instead of a 0 when the incoming variable is blank.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 744 views
  • 3 likes
  • 5 in conversation