BookmarkSubscribeRSS Feed
dmen24
Fluorite | Level 6

Hi,

I need to capitalize the first letter of the word if there is a dash. So in image above, 'Construction -industrial' should be 'Construction - Industrial'.  

dmen24_0-1738696489426.png

Here is my code: 

Concatenate(UpCase(Substring('Industry_Desc'n, 1, 1)),
LowerCase(Substring('Industry_Desc'n, 2, 1000)))

 

 

Can someone help?

 

Thanks!

3 REPLIES 3
Patrick
Opal | Level 21

Please provide more sample data together with desired result via a SAS data step.

Tom
Super User Tom
Super User

It is impossible to code from pictures. So let's use the values you put into your question body instead.

 

Construction -industrial
Construction - Industrial
  • Do you also want to insert that space after the hyphen?
  • What if there is already a space after the hyphen? Do you want to find the next non-space,non-hyphen character and upcase that?
  • Is it possible your text has non-ASCII characters, like an endash or emdash, instead of a hyphen. Do you need to find those also?

So assuming that the hyphen is immediately in front of the character to upcase you could try something like this:

data want;
  set have;
  loc=-1;
  do until(loc=0);
    loc=findc(string,'-',loc+2);
    if loc then substr(string,loc+1,1)=upcase(char(string,loc+1));
  end;
run;

Which is almost exactly like the first example in the FINDC() documentation.

Not hard to adjust to skip over spaces after the hyphen.

data want;
  set have;
  loc=0;
  do until(loc=0);
    loc=findc(string,'-',loc+1);
    if loc then loc=findc(string,'- ',loc+1,'k');
    if loc then substr(string,loc,1)=upcase(char(string,loc));
  end;
run;
Patrick
Opal | Level 21

If below doesn't return what you're after then please provide representative sample data that shows have and want.

data have;
	infile datalines truncover dsd dlm=',';
	input have_str:$60. want_str:$60.;
	datalines;
Construction - industrial sector ,Construction - Industrial sector
Construction - INDUSTRIAL sector ,Construction - INDUSTRIAL sector
Industrial construction ltd      ,Industrial construction ltd
;
run;

data want;
	set have;
	length derived_string $60;
	derived_string=prxchange('s/(- *)([a-z])/$1\u$2/', -1, trim(have_str));
run;

proc print data=want;
run;

Patrick_0-1738718324331.png