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

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

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

18 REPLIES 18
Jagadishkatam
Amethyst | Level 16

We could use the propcase function instead of upcase function.

This function will capitalize the first letter in a word.

Thanks,

Jag

Thanks,
Jag
Dipu
Calcite | Level 5

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

data_null__
Jade | Level 19

Indeed why not UPCASE?

data test;
   input str $20.;
   substr(str,
1,1)=upcase(first(str));
   cards;
this
that.
Hello.
;;;;
   run;
art297
Opal | Level 21

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;

Dipu
Calcite | Level 5

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

Tom
Super User Tom
Super User

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

Show an example.

Dipu
Calcite | Level 5

@@@- 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):(Column).

      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

jakarman
Barite | Level 11

propcase allowed?

---->-- ja karman --<-----
Dipu
Calcite | Level 5

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;

art297
Opal | Level 21

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.

Reeza
Super User

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. 

art297
Opal | Level 21

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

Dipu
Calcite | Level 5

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..

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 9463 views
  • 14 likes
  • 8 in conversation