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
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.
We could use the propcase function instead of upcase function.
This function will capitalize the first letter in a word.
Thanks,
Jag
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
Why not upcase?
Indeed why not 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;
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
What is not working? is it getting an error? not changing anything?
Show an example.
@@@- 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
propcase allowed?
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;
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.
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.
You didn't say what wasn't working. The explanation of the code is fairly simple. The character values can be found at:
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
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.