convert first letter of a string to upper case without using propcase

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 89
Accepted Solution

convert first letter of a string to upper case without using propcase

hi

Please can you tell me how can i convert the first letter of a sting to uppercase without using procase()

data h;

input name$;

cards;

krishna

jizin

gayathri

madhav

neerav

priya

run;


Accepted Solutions
Solution
‎07-21-2014 03:01 AM
Super Contributor
Posts: 297

Re: convert first letter of a string to upper case without using propcase

DATA WANT;

SET HAVE;

NAME2 = UPCASE(SUBSTR(LEFT(NAME),1,1))||SUBSTR(LEFT(NAME),2);

RUN;

View solution in original post


All Replies
Solution
‎07-21-2014 03:01 AM
Super Contributor
Posts: 297

Re: convert first letter of a string to upper case without using propcase

DATA WANT;

SET HAVE;

NAME2 = UPCASE(SUBSTR(LEFT(NAME),1,1))||SUBSTR(LEFT(NAME),2);

RUN;

Frequent Contributor
Posts: 89

Re: convert first letter of a string to upper case without using propcase

Thanks 

Super User
Super User
Posts: 7,407

Re: convert first letter of a string to upper case without using propcase

Just to note, you can modify a character in place, without have to concatenate data back together again, though you won't notice any time diff without lots of rows Smiley Happy

data x;

  attrib res format=$20.;

  res="assfsa";

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

run;

Respected Advisor
Posts: 3,777

Re: convert first letter of a string to upper case without using propcase

I wonder if using FIRST would make any difference

substr(res,1,1)=upcase(first(res));

Super User
Super User
Posts: 7,407

Re: convert first letter of a string to upper case without using propcase

Yes, excellent suggestion.  A few runs of the below two steps shows first using about 0.08 seconds less than the substr (e.g. 0.23 vs 0.31).

data x;

  attrib res format=$20.;

  do i=1 to 2000000;

  res="assfsa";

  substr(res,1,1)=upcase(first(res));

  end;

run;

data y;

  attrib res format=$20.;

  do i=1 to 2000000;

  res="assfsa";

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

  end;

run;

Super User
Posts: 9,682

Re: convert first letter of a string to upper case without using propcase

Or try this one . You gotta love Peal Regular Expression .

data h;
input name$;
cards;
krishna
jizin
gayathri
madhav
neerav
priya
run;
data _null_;
set h;
name = prxchange("s/(\w+)/\u\L$1/i", -1, name);
put name=;
run;


Xia Keshan

New Contributor
Posts: 4

Re: convert first letter of a string to upper case without using propcase

NameHard = upcase(first(Name)) || substr(Name,2,length(Name));

 

You need to get first letter in caps with upcase and first function

After that with substring function get the last letters with length function.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 632 views
  • 1 like
  • 6 in conversation