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

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.

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
ballardw
Super User

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;
Astounding
PROC Star

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;

 

Adambo
Obsidian | Level 7

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

data_null__
Jade | Level 19

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.

 

knveraraju91
Barite | Level 11

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.

 

 

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 10140 views
  • 3 likes
  • 5 in conversation