BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
KrishnaChandra
Calcite | Level 5

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;

1 ACCEPTED SOLUTION

Accepted Solutions
Scott_Mitchell
Quartz | Level 8

DATA WANT;

SET HAVE;

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

RUN;

View solution in original post

8 REPLIES 8
Scott_Mitchell
Quartz | Level 8

DATA WANT;

SET HAVE;

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

RUN;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

data_null__
Jade | Level 19

I wonder if using FIRST would make any difference

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

VISHNU239
Obsidian | Level 7
can anyone please explain me the SUBSTR function in the code?Thank you.
Ksharp
Super User

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

arpi
Fluorite | Level 6

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 4732 views
  • 2 likes
  • 7 in conversation