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.
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;
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;
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;
Brilliant. I actually need this to make medical chart verbatim fit into SAS V5 transport files which have this $200. restriction. Thanks.
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.
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.
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.
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.