BookmarkSubscribeRSS Feed
appleorange
Obsidian | Level 7
data sample2;
 y='icc 1icc ic'; run;

 data sample3; set sample2;
 y2=tranwrd(compress(y, ' '), 'ic', '1icc');
y3=tranwrd(compress(y2, ' '), '1ic', '1icc');
run;

I need to correct the icc -> 1icc

and ic -> 1icc.  The 1icc is correct as is, but if I try to use multiple steps of tranwrd to correct the slight variations, I get 1icccc11icccc1iccc as y3.

 

Is there a way to identify and correct the specific iterations only?

5 REPLIES 5
procphysics
SAS Employee

What do you want your final output y3 to look like?

Do you want it to be '1icc 1icc 1icc'?

appleorange
Obsidian | Level 7
Yes, correct.
procphysics
SAS Employee

The tranwrd function replaces all instances of a character pattern, so when you do

 

y2=tranwrd(compress(y, ' '), 'ic', '1icc');

all instances of 'ic' in the column are replaced with '1icc'.

Also, you are removing the spaces from y so SAS does the tranwrd function on 'icc1iccic'.

y='icc 1icc ic' becomes y2='1iccc11iccc1icc'.

 

If you want your final string to be '1icc 1icc 1icc' then you can do that by breaking up y into each word, then replacing the first and third words.

 

data sample2;
    y='icc 1icc ic';

    firstspace = find(y,' ');
    secondspace = find(y,' ',firstspace+1);

    firstword=substr(y,1,firstspace-1);
    secondword=substr(y,firstspace+1,secondspace-firstspace-1);
    thirdword=substr(y,secondspace+1);

    if firstword='icc' then firstword='1icc';
    if thirdword='ic' then thirdword='1icc';

    y2 = catx(' ',firstword,secondword,thirdword);

 run;

 

 

First use the find function to find the location of the first and second spaces in y. The secondspace looks for the next space after the firstspace.

 

 firstspace = find(y,' ');
 secondspace = find(y,' ',firstspace+1);

 

 

Then use the substr function to extract each word using spaces as delimiters.

 

    firstword=substr(y,1,firstspace-1);
    secondword=substr(y,firstspace+1,secondspace-firstspace-1);
    thirdword=substr(y,secondspace+1);

firstword starts at position 1 and ends right before the first space.

 

secondword is between the first and second spaces.

thirdword starts right after the second space.

 

Now replace the words 'icc' and 'ic' with '1icc'.

   if firstword='icc' then firstword='1icc';
   if thirdword='ic' then thirdword='1icc';

Your new string looks like:

y2 = catx(' ',firstword,secondword,thirdword);

 

Tom
Super User Tom
Super User

If you want to use tranwrd() the include the spaces.  You will have to ensure there is both a leading and trailing space in the string to make sure you catch words at the start or end of the source string.

data test;
  y='ic icc 1icc 1ic';
  length y2 $40 ;
  y2=strip(tranwrd(cat(' ',y, ' '), ' ic ', ' 1icc '));
  y2=strip(tranwrd(cat(' ',y2,' '), ' icc ', ' 1icc '));
  y2=strip(tranwrd(cat(' ',y2,' '), ' 1ic ', ' 1icc '));
run;

Result:

Obs           y                   y2

 1     ic icc 1icc 1ic    1icc 1icc 1icc 1icc
Ksharp
Super User
data test;
  y='ic icc 1icc 1ic';
  length y2 $40 ;
  y2=prxchange('s/\b(ic|icc)\b/1icc/i',-1,y);
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 298 views
  • 0 likes
  • 4 in conversation