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

Solved
Frequent Contributor
Posts: 89

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

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;

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

Thanks

Super User
Posts: 8,798

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

data x;

attrib res format=\$20.;

res="assfsa";

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

run;

Posts: 3,811

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
Posts: 8,798

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;

Occasional Contributor
Posts: 17

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

can anyone please explain me the SUBSTR function in the code?Thank you.
Super User
Posts: 10,416

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
neerav
priya
run;
data _null_;
set h;
name = prxchange("s/(\w+)/\u\L\$1/i", -1, name);
put name=;
run;

```

Xia Keshan

Occasional Contributor
Posts: 5

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.