@Nira wrote:
My input string length 500. In that String 3 variables need to be replaced. Since TRANWRD allows only 200 , from 201 positiong string is not displayed in the output.
See my previous post. That should explain it to you.
If you use syntax like: new_variable = tranwrd(....); and this is the first time that new_variable has been listed in your code then this new_variable gets created with a default length of 200 - which in your case is too short and the source string gets truncated.
In such cases: Use a length statement at the beginning of your code -> length new_variable $500; Now you've got what you need and things will work.
@Nira wrote:
Can anybody help to solve ? Since TRANWRD function limits to only 200 characters. Thanks
RTM
Length of Returned Variable
In a DATA step, if the TRANWRD function returns a value to a variable that has not previously been assigned a length, then that variable is given a length of 200bytes. You can use the LENGTH statement, before calling TRANWRD, to change the length of the value.
@Nira wrote:
Can anybody help to solve ? Since TRANWRD function limits to only 200 characters. Thanks
That is not true (unless you are using SAS version 6). Why do you think that there is such a limit?
@Nira wrote:
Could you send me any sample using length
See here: LENGTH Statement
> Did I understand your point correctly?
Yes. That's what the manual says too.
> But got error as variables within regular expressions are not supported
You may not be ready to use Regular Expressions yet.
This is *not* one of the first things a programmer learns, and you seem to be a novice programmer.
TRANWRD() works for variables longer than 200 characters. It's just that if you use the tranwrd() function and assign the result to a new variable then this variable gets by default created with a length of 200 characters. That's why in below code variable want_string gets first created with a length statement so it can hold the full string.
data have(keep=have_string);
infile datalines truncover;
input str $100.;
length have_string $500;
retain have_string;
have_string=catx(' ', have_string,str);
if _n_=5 then output;
datalines;
Replace from one word to another word in a long string of more than 200 characters
You are saying that WORD if that variable length is not defined previously then it will take
length as 200 bytes. So you said to use WORD length function before tranwrd function.
Did I understand your point correctly?
But got error as variables within WORD regular expressions are not supported
;
data want;
set have;
length want_string $500;
want_string=tranwrd(have_string,'WORD','OTHERWORD');
run;
proc print data=want;
run;
@Nira wrote:
My input string length 500. In that String 3 variables need to be replaced. Since TRANWRD allows only 200 , from 201 positiong string is not displayed in the output.
See my previous post. That should explain it to you.
If you use syntax like: new_variable = tranwrd(....); and this is the first time that new_variable has been listed in your code then this new_variable gets created with a default length of 200 - which in your case is too short and the source string gets truncated.
In such cases: Use a length statement at the beginning of your code -> length new_variable $500; Now you've got what you need and things will work.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.