BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nira
Calcite | Level 5
Replace from one word to another word in a long string of more than 200 characters
1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@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.

View solution in original post

19 REPLIES 19
Nira
Calcite | Level 5
Can anybody help to solve ? Since TRANWRD function limits to only 200 characters. Thanks
data_null__
Jade | Level 19

@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.

Tom
Super User Tom
Super User

@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?

ed_sas_member
Meteorite | Level 14

Hi @Nira

Maybe you can try to use a pearl regular expression :

Var2 = Prxchange('s/ /*expr to find*/ / /*replacement text*/ /', -1, Var1);


Best,

Nira
Calcite | Level 5
Hi
Sorry I don’t understand Could you explain that?
Nira
Calcite | Level 5
Could you send me any sample using length
Nira
Calcite | Level 5
You are saying that if that variable length is not defined previously then it will take length as 200 bytes. So you said to use length function before tranwrd function. Did I understand your point correctly?
ChrisNZ
Tourmaline | Level 20

> Did I understand your point correctly?

 

Yes. That's what the manual says too.

Nira
Calcite | Level 5
I code as you said
Var2 = PRXCHANGE(‘s/ $ DTEAPNTMT / Strip(APNTMT_DATE) /‘, 1, TXT);

But got error as variables within regular expressions are not supported
ChrisNZ
Tourmaline | Level 20

> 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.

Patrick
Opal | Level 21

@Nira 

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
Calcite | Level 5
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.
Patrick
Opal | Level 21

@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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 19 replies
  • 3813 views
  • 0 likes
  • 7 in conversation