Converting the case of letters in a variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Converting the case of letters in a variable

Hello,

  I just require the First letter in varible value in Uppercase and 2nd letter in variable value should be in lower case, it should continue till the end letter of the variable value.

/********************I have a the data like this *****************/

  Data Have;
    Input Name $ 30.;
    cards;
    abcdefhg
    ghfedcba
    IJKLMNOP
    qtunvjrf
    FgYhbder
;

/************************The output Should be like below**************/

NAME
AbCdEfGh
GhFeDcBa
IjKlMnOp
QtUnVjRf
FgThBdEr

Please help me on this .


Accepted Solutions
Solution
‎05-04-2015 09:42 AM
Super User
Posts: 9,681

Re: Converting the case of letters in a variable

Very interesting question.

I am wondering why you behavior this way ?

Data Have;
    Input Name $ 30.;
cards;
abcdefhg
ghfedcba
IJKLMNOPI
qtunvjrf
FgYhbder
;
run;
data want;
 set have;
 new=upcase(name);
 do i=2 to length(name) by 2;
  substr(new,i,1)=lowcase(substr(new,i,1));
 end;
 drop i;
run;

Xia Keshan

View solution in original post


All Replies
Valued Guide
Posts: 858

Re: Converting the case of letters in a variable

look up propcase, that'll do the trick for you.  I can't test it now but something like this:

word1 = propcase(word);

Super Contributor
Posts: 305

Re: Converting the case of letters in a variable

Hello,

Data Have;
    Input Name $ 30.;
cards;
abcdefhg
ghfedcba
IJKLMNOPI
qtunvjrf
FgYhbder
run;


data want;
length _c $ 30 _a $ 1;

if _n_=1 then
do;   
declare hash h(ordered:'Y');
  h.definekey('i');
    h.definedata('_a');
    h.definedone();
call missing(_a);
    declare hiter hi('h');
end;

i=1;
set have;
     do while(substr(name,i,1) ne ' ');
     if mod(i,2) then _a=upcase(substr(strip(name),i,1));
     else _a=lowcase(substr(strip(name),i,1));
           _rc=h.add();
     i=i+1;
     end;

do _rc = hi.first() by 0 while (_rc = 0);

_c=strip(_c)||strip(_a);
_rc = hi.next();
end;

h.clear();

keep name _c;
run;

Valued Guide
Posts: 858

Re: Converting the case of letters in a variable

I don't know much about hash, is there a book out there that you prefer?  I have "SAS Hash object programming made easy"  I've been crazy busy at work so I haven't gone through much but I like it so far.  Anything else you'd recommend?

Super Contributor
Posts: 305

Re: Converting the case of letters in a variable

Hello,

Here are some useful links:

https://communities.sas.com/message/180853#180853

If you have the book in electronic format could you please send it to my email address - potosi2345@gmail.com ?

10x

Valued Guide
Posts: 858

Re: Converting the case of letters in a variable

I bought a traditional paperback, not an electronic version.  I'll have to look to see if the electronic is included, if so I'll send it your way.

Solution
‎05-04-2015 09:42 AM
Super User
Posts: 9,681

Re: Converting the case of letters in a variable

Very interesting question.

I am wondering why you behavior this way ?

Data Have;
    Input Name $ 30.;
cards;
abcdefhg
ghfedcba
IJKLMNOPI
qtunvjrf
FgYhbder
;
run;
data want;
 set have;
 new=upcase(name);
 do i=2 to length(name) by 2;
  substr(new,i,1)=lowcase(substr(new,i,1));
 end;
 drop i;
run;

Xia Keshan

Super Contributor
Posts: 275

Re: Converting the case of letters in a variable

Data Have;

    Input Name $ 30.;

    _name=prxchange('s/(\w)(\w)/\U$1\L$2/i',-1,name);

    cards;

    abcdefhg

    ghfedcba

    IJKLMNOP

    qtunvjrf

    FgYhbder

;

run;

Occasional Contributor
Posts: 8

Re: Converting the case of letters in a variable

Really today you guys made my life easy.

Thanks Loko, Mark Johnson, Xia Keshan and Slchen for the valueble suggetions and fast response on this discussion.

Super User
Posts: 9,681

Re: Converting the case of letters in a variable

Your code only work on even length of string .

cards;

    abcdefh

    ghfedcba

    IJKLMNOP

    qtunvjrf

    FgYhbder

;

run;

Super Contributor
Posts: 275

Re: Converting the case of letters in a variable

You are right, if not even length of string, it should be modified as

Data Have;

    Input Name $ 30.;

    _name=prxchange('s/(\w)(\w)?/\U$1\L$2/i',-1,name);

    cards;

     abcdefh

    ghfedcba

    IJKLMNOP

    qtunvjrf

    FgYhbde

;

run;

Respected Advisor
Posts: 3,124

Re: Converting the case of letters in a variable

Slightly simplified version:

Data Have;

  Input Name $ 30.;

new_name=prxchange('s/(\w\w?)/\u\l$1/i', -1,name);

  cards;

  abcdefhgf

  ghfedcba

  IJKLMNOP

  qtunvjrf

  FgYhbder

;

☑ This topic is SOLVED.

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

Discussion stats
  • 11 replies
  • 283 views
  • 4 likes
  • 6 in conversation