BookmarkSubscribeRSS Feed
_el_doredo
Quartz | Level 8

Hi Experts,
I have one doubt .. I hope you guys can clarify my doubt..

%let string=the quick brown fox jumps over the lazy dog; /*all low case letters*/


This is the string, now I need to update all the second occurrence of 'o' letter to capital one... how I will achieve it? Any ideas??

 

My output should be like this

"the quick brown fOx jumps over the lazy dOg"

 

Thanks in Advance

7 REPLIES 7
PaigeMiller
Diamond | Level 26

My output should be like this

"the quick brown fOx jumps over the lazy dOg"

Should that be: the quick brown fOx jumps Over the lazy dOg  ??

 

You can use the TRANSLATE function:

 

data have;
    text='the quick brown fox jumps over the lazy dog';
run;

data want;
    set have;
    location_of_first_letter_o = find(text,'o');
    substr(text,location_of_first_letter_o+1)=translate(substr(text,location_of_first_letter_o+1),'O','o');
run;

 

--
Paige Miller
_el_doredo
Quartz | Level 8

@PaigeMiller  , Thanks for your answer. But, I need alternate 'o' to be upper case

PaigeMiller
Diamond | Level 26

In that case, I think you have to loop over the entire character string to identify the even numbered occurrences of letter 'o'.

 

data want;
    set have;
    location_of_letter_o=0;
    count=1;
    do while (find(substr(text,location_of_letter_o+1),'o')>0);
        location_of_letter_o=location_of_letter_o+find(substr(text,location_of_letter_o+1),'o');
        if mod(count,2)=0 then substr(text,location_of_letter_o,1)='O';
        count=count+1;
    end;
run;
--
Paige Miller
_el_doredo
Quartz | Level 8

@PaigeMiller Thank you so much.. I'll work on this one and i'll learn about it

Ksharp
Super User
data have;
    text='the quick brown fox jumps over the lazy dog';
run;

data want;
    set have;
want=prxchange('s/(o[^o]*)(o)/\1\U\2/',-1,text);
run;

proc print;run;
_el_doredo
Quartz | Level 8

@Ksharp ,Thank you so much. But, Can you please explain about your code. I know that prxchange will check based on the bracket. But, Not getting completely. So it would be helpful if you explain your code

 

Thanks in advance

Ksharp
Super User

(o[^o]*)(o)
-->
stand for looking for two part (o[^o]*) and (o).
first part , o means 'o' , [^o] means any character which is not 'o' , * means match [^o] zero or more times.
second part is easy ,just match 'o' .

Put it all together is looking for a string which have two 'o' .


\1\U\2
-->
\1 means the first matched part (o[^o]*)
\U means upper case the following character
\2 means the second matched part (o)

 

 

 

For Example:

the first looking is 

own fo
The first part is (own f) ,the second part is (o) , \U will uppercase the second part (o) , so the result is 'own fO'

the second looking is as the same:

over the lazy do
The first part is (over the lazy d) ,the second part is (o) , \U will uppercase the second part (o) , so the result is 'over the lazy dO'

 

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 7 replies
  • 994 views
  • 0 likes
  • 3 in conversation