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

Hi Everyone:

I have a data set that looks like this:

ABC INVESTMENT TR CO TR

ABC WORLD CO TRANSPORTATION

ABC SKYE INVESTMENT TR

ABC CLOUD PROPERTY TR

ABC MOVIES PRODUCTION TRUSTME

TRUST YOURSELF TR TRUE TRACTOR

 

Strings vary in length. My goal is to replace "TR" at the end of the string with "TRUST". I want to do this if and only if the last word is TR. Ideally, the data will look like this:

ABC INVESTMENT TR CO TRUST

ABC WORLD CO TRANSPORTATION

ABC SKYE INVESTMENT TRUST

ABC CLOUD PROPERTY TRUST

ABC MOVIES PRODUCTION TRUSTME

TRUST YOURSELF TR TRUE TRACTOR

I used tranwrd successfully to replace other parts of the strings, but got stuck on the last one. Could you help me with this? I am using SAS 9.4.

 

Thank you so much!

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

One way that uses the fact this is the LAST "word".

data example;
   length x $ 100;
   x="ABC INVESTMENT TR CO TR";
   if scan(x,countw(x))='TR' then x=cats(x,'UST');
run;

Potential issue. If your current variable length is such that adding 3 characters would exceed the length of the variable then the value would be truncated.

data example;
   length x $ 25;
   x="ABC INVESTMENT TR CO TR";
   if scan(x,countw(x))='TR' then x=cats(x,'UST');
run;

So you may have to create a new variable with a longer defined length.

View solution in original post

5 REPLIES 5
ballardw
Super User

One way that uses the fact this is the LAST "word".

data example;
   length x $ 100;
   x="ABC INVESTMENT TR CO TR";
   if scan(x,countw(x))='TR' then x=cats(x,'UST');
run;

Potential issue. If your current variable length is such that adding 3 characters would exceed the length of the variable then the value would be truncated.

data example;
   length x $ 25;
   x="ABC INVESTMENT TR CO TR";
   if scan(x,countw(x))='TR' then x=cats(x,'UST');
run;

So you may have to create a new variable with a longer defined length.

finans_sas
Quartz | Level 8
Thank you so much, ballardw!
Tom
Super User Tom
Super User

Just changing it at the end of the lines make it easy.  You can just append something to the end of the string and to the end of the string you are asking TRANWRD() to change.

data want;
  set have;
  if scan(string,-1,' ')='TR' then do;
    string=tranwrd(cats(string,'^%|'),'TR^%|','TRUST');
  end;
run;

Otherwise learn how to use the regular expression functions.

finans_sas
Quartz | Level 8
Thank you so much, Tom!
Ksharp
Super User

Just for some fun .

 

data example;
   length x $ 100;
   x="ABC INVESTMENT TR CO TR";
   y=prxchange('s/\btr$/TRUST/i',1,strip(x));
   output;

      x="ABC WORLD CO TRANSPORTATION";
   y=prxchange('s/\btr$/TRUST/i',1,strip(x));
   output;

run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 1698 views
  • 4 likes
  • 4 in conversation