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

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 .

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

11 REPLIES 11
Steelers_In_DC
Barite | Level 11

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

word1 = propcase(word);

Loko
Barite | Level 11

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;

Steelers_In_DC
Barite | Level 11

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?

Loko
Barite | Level 11

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

Steelers_In_DC
Barite | Level 11

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.

Ksharp
Super User

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

slchen
Lapis Lazuli | Level 10

Data Have;

    Input Name $ 30.;

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

    cards;

    abcdefhg

    ghfedcba

    IJKLMNOP

    qtunvjrf

    FgYhbder

;

run;

Daya87
Calcite | Level 5

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.

Ksharp
Super User

Your code only work on even length of string .

cards;

    abcdefh

    ghfedcba

    IJKLMNOP

    qtunvjrf

    FgYhbder

;

run;

slchen
Lapis Lazuli | Level 10

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;

Haikuo
Onyx | Level 15

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

;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 11 replies
  • 929 views
  • 4 likes
  • 6 in conversation