BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10
/* Using PROC SQL */
proc sql;
  
  select 
         substr(Name, 1, 1) || propcase(substr(Name, 2, 1)) || substr(Name, 3) as NewVariable
  from sashelp.class;
quit;

how to capitalized 2nd letter using array and do loop in datastep method

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Not sure why you would use a loop (or why you are using PROPCASE() instead of UPCASE()).

Just replace the 2 letter with its uppercase version.

data want;
  set sashelp.class;
  substr(name,2,1)=upcase(substr(name,2,1));
run;

The only reason to use an ARRAY would be if there were multiple variables you wanted to do the same operation on.   In that case watch out for variables that do not have room for a second letter.

data want;
  set sashelp.class;
  array c _character_;
  do over c;
    if vlength(c) > 1 then substr(c,2,1)=upcase(substr(c,2,1));
  end;
run;

If you don't like using features SAS is trying to disavow (This tape will self-destruct in 5 seconds...) then introduce an index variable to the DO loop and the array references.

data want;
  set sashelp.class;
  array c _character_;
  do index=1 to dim(c);
    if vlength(c[index]) > 1 then substr(c[index],2,1)=upcase(substr(c[index],2,1));
  end;
  drop index;
run;

 

View solution in original post

5 REPLIES 5
Tom
Super User Tom
Super User

Not sure why you would use a loop (or why you are using PROPCASE() instead of UPCASE()).

Just replace the 2 letter with its uppercase version.

data want;
  set sashelp.class;
  substr(name,2,1)=upcase(substr(name,2,1));
run;

The only reason to use an ARRAY would be if there were multiple variables you wanted to do the same operation on.   In that case watch out for variables that do not have room for a second letter.

data want;
  set sashelp.class;
  array c _character_;
  do over c;
    if vlength(c) > 1 then substr(c,2,1)=upcase(substr(c,2,1));
  end;
run;

If you don't like using features SAS is trying to disavow (This tape will self-destruct in 5 seconds...) then introduce an index variable to the DO loop and the array references.

data want;
  set sashelp.class;
  array c _character_;
  do index=1 to dim(c);
    if vlength(c[index]) > 1 then substr(c[index],2,1)=upcase(substr(c[index],2,1));
  end;
  drop index;
run;

 

BrahmanandaRao
Lapis Lazuli | Level 10

Hi Tom,

Thank you very much your different solutions

suppose i want to capitalized 2nd and last letters how can solve  substr not working for last letter 

data;
set sashelp.class;
substr(name,2,1)=upcase(substr(name,2,1)); 
substr(name,-1,1)=upcase(substr(name,-1,1));
run;
Tom
Super User Tom
Super User

SUBSTR() works fine for the last character.  You just can't used negative positions as that has no meaning.

substr(name,length(name),1)=upcase(substr(name,length(name),1));
BrahmanandaRao
Lapis Lazuli | Level 10
data fl;
set sashelp.class;
substr(name,2,1)=upcase(substr(name,2,1)); 
substr(name,length(name),1)=upcase(substr(name,length(name),1));
proc print noobs;
run;

Thanks Tom 

for Your smart solutions

Patrick
Opal | Level 21

Just for fun adding a RegEx option. It also covers your later requirement to uppercase the last character in the string.

data test;
  length source target $5;
  do source=' ','a','aa','aaa','aaaa','aaaaa';
    target=prxchange('s/^(.)(.)(.*?)(.?)$/$1\u$2$3\u$4/oi',1,strip(source));
    output;
  end;
run;
proc print data=test;
run;

Patrick_0-1690092124119.png

 

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 5 replies
  • 937 views
  • 1 like
  • 3 in conversation