Help using Base SAS procedures

replacing text causes string to cut off

Accepted Solution Solved
Reply
Super Contributor
Posts: 400
Accepted Solution

replacing text causes string to cut off

Hello,

I have some html text in one of my data sets.  I need to replace all occurrences of 'Temp' to 'Temporary'.

When I do the following it cuts the string off.

txt = tranwrd(temptext, 'Temp', 'Temporary') ;

This is my full html string

<tr>

<td>1</td>

<td>Temp 1</td>

<td>07/23/2015</td>

</tr>

<tr>

<td>2</td>

<td>Temp 4</td>

<td>03/05/2016</td>

</tr>

<tr>

<td>3</td>

<td>Temp 8</td>

<td>04/13/2017</td>

</tr>

after running the above code txt variable only has

<tr>

<td>1</td>

<td>Temp 1</td>

<td>07/23/2015</td>

</tr>

I'm not sure why this is happening.

Thank you for any help.


Accepted Solutions
Solution
‎01-27-2015 09:45 AM
Respected Advisor
Posts: 3,156

Re: replacing text causes string to cut off

Posted in reply to jerry898969

Works for me:

data _null_;

temptext='<td>Temp 1</td>  ';

txt=tranwrd(temptext, 'Temp', 'Temporary') ;

put temptext= / txt=;

run;

What is the EXACT code you used?

Update: Oh, the cut-off is probably due to the length setting of your new variable 'txt', if not specified, the default is 200. So assign a proper length before process the string.

Update2: Here is to vent a little. This is one inconsistency that I feel SAS can improve. Some functions will take the length of precessed variable (such as SUBSTR(), which I like), Some will take something else, such as this one: 200. I know this is a legacy issue resulting from different developers and is common seen in many other programming languages as well, but I still feel SAS can do better to further lift those stress put on the shoulder of programmers. Similar inconsistency also happened to TRANWRD VS TRANSLATE regarding how they align their arguments. The order differences of 'source' and 'target' makes me quit remembering. 

Haikuo

View solution in original post


All Replies
Solution
‎01-27-2015 09:45 AM
Respected Advisor
Posts: 3,156

Re: replacing text causes string to cut off

Posted in reply to jerry898969

Works for me:

data _null_;

temptext='<td>Temp 1</td>  ';

txt=tranwrd(temptext, 'Temp', 'Temporary') ;

put temptext= / txt=;

run;

What is the EXACT code you used?

Update: Oh, the cut-off is probably due to the length setting of your new variable 'txt', if not specified, the default is 200. So assign a proper length before process the string.

Update2: Here is to vent a little. This is one inconsistency that I feel SAS can improve. Some functions will take the length of precessed variable (such as SUBSTR(), which I like), Some will take something else, such as this one: 200. I know this is a legacy issue resulting from different developers and is common seen in many other programming languages as well, but I still feel SAS can do better to further lift those stress put on the shoulder of programmers. Similar inconsistency also happened to TRANWRD VS TRANSLATE regarding how they align their arguments. The order differences of 'source' and 'target' makes me quit remembering. 

Haikuo

Super Contributor
Posts: 400

Re: replacing text causes string to cut off

Hi Hai.kou,

Thank you so much.  I didn't think of adding the length statement to my code. 

That fixed it. 

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 516 views
  • 0 likes
  • 2 in conversation