Convert First letter of string to upper case WITHOUT using upcase

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

Convert First letter of string to upper case WITHOUT using upcase

Hello All,

Can you please suggest me-

How to convert the first letter of string to upper case without using upcase function.The data is in SAS dataset.

Thank you


Accepted Solutions
Solution
‎03-28-2014 03:26 PM
PROC Star
Posts: 7,356

Re: Convert First letter of string to upper case WITHOUT using upcase

It isn't working for you because your file has a variable called "name"  while my example had a variable called "string".  You would have to change;

if rank(substr(string,1,1)) in (97:122) then

    substr(string,1,1)=byte(rank(substr(string,1,1))-32);

to

if rank(substr(name,1,1)) in (97:122) then

    substr(name,1,1)=byte(rank(substr(name,1,1))-32);

I'm also assuming that you are on an ASCII-based system like windows.  Otherwise, the values would have to be changed to reflect EBCIDIC.

View solution in original post


All Replies
Trusted Advisor
Posts: 1,128

Re: Convert First letter of string to upper case WITHOUT using upcase

We could use the propcase function instead of upcase function.

This function will capitalize the first letter in a word.

Thanks,

Jag

Thanks,
Jag
Contributor
Posts: 43

Re: Convert First letter of string to upper case WITHOUT using upcase

Hi Jagdish thank you for the prompt response.

unfortunately I can not use Propcase as well.Please suggest me other option, if we have

Thank you

Deepak

Super User
Posts: 17,734

Re: Convert First letter of string to upper case WITHOUT using upcase

Why not upcase?

Respected Advisor
Posts: 3,777

Re: Convert First letter of string to upper case WITHOUT using upcase

Indeed why not UPCASE?

data test;
   input str $20.;
   substr(str,
1,1)=upcase(first(str));
   cards;
this
that.
Hello.
;;;;
   run;
PROC Star
Posts: 7,356

Re: Convert First letter of string to upper case WITHOUT using upcase

Sounds like a class project.  Regardless, you can always mimic the upcase function using the byte and rank functions.  e.g.:

data have;

  input string $50.;

  cards;

this is Garbage

More garbage

buNch of Characters

;

data want;

  set have;

  if rank(substr(string,1,1)) in (97:122) then

    substr(string,1,1)=byte(rank(substr(string,1,1))-32);

run;

Contributor
Posts: 43

Re: Convert First letter of string to upper case WITHOUT using upcase

Hi Arthur,

This is awesome but again it is not working on my dataset.

Before writing errors which I am getting can you please throw some light, what exactly you did? I am unable to understand those figures. Please guide

@Reeza- Even I don't know. why not upcase, I assume upcase will convert the entire string.

Thank you

Super User
Super User
Posts: 6,495

Re: Convert First letter of string to upper case WITHOUT using upcase

What is not working? is it getting an error? not changing anything?

Show an example.

Contributor
Posts: 43

Re: Convert First letter of string to upper case WITHOUT using upcase

@@@- This is what I tried!!

Please guide-

data j;

input name $20.;

cards;

mahesh

deepak

alex

susan

maxia

run;

data J1 ;

  set J;

  if rank(substr(string,1,1)) in (97:122) then

    substr(string,1,1)=byte(rank(substr(string,1,1))-32);

run;

ERRRORRRR******

66   data J1 ;

67     set J;

68     if rank(substr(string,1,1)) in (97:122) then

69       substr(string,1,1)=byte(rank(substr(string,1,1))-32);

                -

                356

ERROR: Argument to SUBSTR must be character.

NOTE 356-185: The SUBSTR pseudo-variable function does not allow character constants,

              expressions, or numeric constants for the first argument.

70   run;

NOTE: Numeric values have been converted to character values at the places given by:

      (Line)Smiley SadColumn).

      68:18   69:41

NOTE: The SAS System stopped processing this step because of errors.

WARNING: The data set WORK.J1 may be incomplete.  When this step was stopped there were 0

         observations and 2 variables.

WARNING: Data set WORK.J1 was not replaced because this step was stopped.

NOTE: DATA statement used (Total process time):

      real time           0.01 seconds

      cpu time            0.00 seconds

Valued Guide
Posts: 3,208

Re: Convert First letter of string to upper case WITHOUT using upcase

propcase allowed?

---->-- ja karman --<-----
Contributor
Posts: 43

Re: Convert First letter of string to upper case WITHOUT using upcase

Propcase is not allowed. Thats the problem-

Though What Arthur has suggested, it works but not in my case, as mentioned above. (Please explain that step with RANK and BYTES and how did you calculate those numbers--97:122 and -32)
secondly @data_null_ has also suggested, it will work but looking for w/o propcase and upcase.

Even this works-                                       But as I said  looking for alternative

data j;

input name $50.;

cards;

lavanya

daniel

nandu

rachel

vandana

run;

data J1;

  set j;

 

Length name_newvalues $50.;

lenstr=length(trim(name));

name_newvalue=upcase(substr(trim(name),1,1))||substr(trim(name),2,lenstr-1);

run;

Solution
‎03-28-2014 03:26 PM
PROC Star
Posts: 7,356

Re: Convert First letter of string to upper case WITHOUT using upcase

It isn't working for you because your file has a variable called "name"  while my example had a variable called "string".  You would have to change;

if rank(substr(string,1,1)) in (97:122) then

    substr(string,1,1)=byte(rank(substr(string,1,1))-32);

to

if rank(substr(name,1,1)) in (97:122) then

    substr(name,1,1)=byte(rank(substr(name,1,1))-32);

I'm also assuming that you are on an ASCII-based system like windows.  Otherwise, the values would have to be changed to reflect EBCIDIC.

Super User
Posts: 17,734

Re: Convert First letter of string to upper case WITHOUT using upcase

So this isn't really a practical question then, more of a homework assignment?

Substring the first letter out, use translate function to switch lower case to upper case and concatenate back. 

PROC Star
Posts: 7,356

Re: Convert First letter of string to upper case WITHOUT using upcase

You didn't say what wasn't working.  The explanation of the code is fairly simple.  The character values can be found at:

Table of ASCII Characters

Look at the decimal values of A-Z and a-z.

Rank function:SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Byte function: SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Contributor
Posts: 43

Re: Convert First letter of string to upper case WITHOUT using upcase

Thank you for the explanation. I would like to go through the links first (Very well answered).

-- Actually, I have assigned this task by the data management (Oracle) Dpt of my company.

They can do this with oracle as well but they wanted to do it in SAS. Thanks though and also concatenation is the great approach

Thank you all of you..

☑ This topic is SOLVED.

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

Discussion stats
  • 18 replies
  • 1906 views
  • 13 likes
  • 8 in conversation