DATA Step, Macro, Functions and more

sentence case

Reply
Contributor VRD
Contributor
Posts: 33

sentence case

I want to make sentence case  through out my variables.

For ex: in 3 parts of my paper there are remarks. please go through them.

This should become like this

In 3 parts of my paper there are remarks. Please go through them.

How can this be done.

Respected Advisor
Posts: 4,173

Re: sentence case

One way to go:

data _null_;

  have='in 3 parts of my paper there are remarks. Have you read them? please go through them! then send it back to me.';

  want=prxchange('s/(^ *.)|([\.\?!] +.)/\U\1\2\E/oi',-1,have);

  put want=;

run;

Contributor VRD
Contributor
Posts: 33

Re: sentence case

i do not have any other signs other than .(full stop).

will it still work because i am getting all in upcase after  using patricks code

Super User
Posts: 7,760

Re: sentence case

You must have made an error in copying the code. here's my log:

16     data _null_;

17       have='in 3 parts of my paper there are remarks. Have you read them. please go through them. then send it back to me.';

18       want=prxchange('s/(^ *.)|([\.\?!] +.)/\U\1\2\E/oi',-1,have);

19       put want=;

20     run;

want=In 3 parts of my paper there are remarks. Have you read them. Please go through them. Then send it back to me.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 10,020

Re: sentence case

Patrick

Your Perl Regular Expression is more and more better now . Are you working on it for a long time ?

BTW,  \1\2\ stand for what ?


Xia Keshan

Respected Advisor
Posts: 3,156

Re: sentence case

To refer first and second buffer. Same as using $1$2.

Respected Advisor
Posts: 4,173

Re: sentence case

Thanks . And no, didn't take me that long.

Amending this based on 's explanation:

Hi

I love the power of regular expressions even though the syntax is "cryptic" and I have still almost always to consult the metacharacter page SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition to get it right.

In real life implementations I don't have that many cases where I use regular expressions - but if I have such a case then using regular expressions makes a difference. Most of the time it's related to data cleansing tasks.

Respected Advisor
Posts: 3,156

Re: sentence case

,

Being as same Chinese as  ,   Please allow me to interpret what he really means Smiley Happy:

Are you working on it for a long time ? = Are you using PRX a lot?

Haikuo

Super Contributor
Posts: 308

Re: sentence case

Hello,

Another one:

data _null_;

length y $ 50;

x="in 3 parts of my paper there are remarks. please go through them.";

i+1;

do until(scan(x,i,".") eq "");

y=cats(upcase(substr(left(scan(x,i,".")),1,1)),substr(left(scan(x,i,".")),2));

x=left(tranwrd(x,scan(x,i,"."),cat(" ",strip(y))));

i+1;

put x= ;

end;

run;

Contributor VRD
Contributor
Posts: 33

Re: sentence case

unfortunately they are not working

Super Contributor
Posts: 345

Re: sentence case

Tried the prxchange solution provided by Patrick, and the output was:

want=In 3 parts of my paper there are remarks. Have you read them? Please go through them! Then send it back to me.

Please explain what does not work.

Super Contributor
Posts: 308

Re: sentence case

hello,

what do you mean by "not working" ? the output is:

x=In 3 parts of my paper there are remarks. Please go through them.

Super User
Posts: 10,020

Re: sentence case

Here is another way other than Perl.

data _null_;

  have='in 3 parts of my paper there are remarks. Have you read them? please go through them! then send it back to me.';

  length want temp punc $ 200;

  punc=compress(have,'.?!','k');

  do i=1 to countw(have,'.?!') ;

   temp=strip(scan(have,i,'.?!')); put temp=;

   substr(temp,1,1)=upcase(substr(temp,1,1)) ;

   want=cats(want,temp,substr(punc,i,1));

  end;

  put want=;

run;

Xia Keshan

Super User
Posts: 10,020

Re: sentence case

Here is my PRX.

data _null_;

  have='in 3 parts of my paper there are remarks. Have you read them? please go through them! then send it back to me.';

  want=prxchange('s/((^\s*\w)|(?<=\.|\?|!)(\s*\w))/\U$1\E/',-1,have);

  put want=;

run;

Xia Keshan

Contributor VRD
Contributor
Posts: 33

Re: sentence case

Hi all,

I somehow cant get it cracked as for my variable i still get few observations with upcase

ex: SMALL PARTICLES IN THE RESERVOIR, APPEAR TO BE SILICONE PARTICLES

is it because there is no comma or full stop which is not working in this case

Ask a Question
Discussion stats
  • 17 replies
  • 1244 views
  • 1 like
  • 8 in conversation