BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
BrahmanandaRao
Lapis Lazuli | Level 10

HI Good Morning 

 

I wan to below output is 

 

 

OUTPUT:

data test;
x='sas is the leader in analytics';
put x;
run;

SaS iS tHE lEaDeR iN aNaLyTiCs

1 ACCEPTED SOLUTION

Accepted Solutions
unison
Lapis Lazuli | Level 10
data test;
format x $50.;
x='sas is the leader in analytics';
do i=1 to length(x);
	if mod(i,2)>0 then substr(x,i,1)= upcase(substr(x,i,1));
end;
run;

-unison

-unison

View solution in original post

14 REPLIES 14
Reeza
Super User

Not sure why you'd want to do this but LENGTH() will give you the length of the string, CHAR() with an index will give you the specific character and you can change the case using UPCASE()/LOWCASE() to change the case.

You need a do loop to loop over the characters.

 


@BrahmanandaRao wrote:

HI Good Morning 

 

I wan to below output is 

 

 

OUTPUT:

data test;
x='sas is the leader in analytics';
put x;
run;

SaS iS tHE lEaDeR iN aNaLyTiCs


 

BrahmanandaRao
Lapis Lazuli | Level 10

Hi Reeza

This is interview question

can we solve it

Reeza
Super User

@BrahmanandaRao wrote:

Hi Reeza

This is interview question

can we solve it


You're assuming your interviewer can't see this page by the way. 

unison
Lapis Lazuli | Level 10
data test;
format x $50.;
x='sas is the leader in analytics';
do i=1 to length(x);
	if mod(i,2)>0 then substr(x,i,1)= upcase(substr(x,i,1));
end;
run;

-unison

-unison
BrahmanandaRao
Lapis Lazuli | Level 10

Hi Unison

 

Thank you very much  brilliant code Smiley Happy

ChrisNZ
Tourmaline | Level 20

Sightly simpler:

if mod(I,2) then substr(X,I,1)= upcase(char(X,I));

 

art297
Opal | Level 21

Here is one way:

data test;
  length word $20.;
  length y $80.;
  x='sas is the leader in analytics';
  n=1;
  do until (missing(scan(x,n)));
    word=scan(x,n);
    if not missing(word) then do;
      if n eq 1 then do;
        substr(word,1,1) = upcase(substr(word,1,1));
        substr(word,3,1) = upcase(substr(word,3,1));
      end;
      else if n eq 3 then do;
      substr(word,2,1) = upcase(substr(word,2,1));
      substr(word,3,1) = upcase(substr(word,3,1));
      end;
      else do i=2 to length(word) by 2;
        substr(word,i,1) = upcase(substr(word,i,1));
      end;
      put word;
      y=catx(' ',y,word);
      n+1;
    end;
  end;
  put x;
  put y;
run;

Art, CEO, AnalystFinder.com

 

BrahmanandaRao
Lapis Lazuli | Level 10

Than You sir support 

 

your coding skills are par excellence

 

 

SASKiwi
PROC Star

Just my two cents worth - I personally can't see the point of interview questions about things you would never do in a real job. If you want to test a candidate's string manipulation skills then choose real-life examples like names, addresses, job descriptions etc.

Reeza
Super User
Maybe, This is a decent question to test if a candidate has the skills they claim. It's basic enough that most programmers should understand it and it's quick without having to ask many questions. This is something a beginner SAS programmer should be able to answer in less than 30 minutes. Some orgs have limits on what we can test - my current one only allows me an hour, where the previous role I used to ask for a take home test that was definitely time intensive.
Ksharp
Super User
data test;
x='sas is the leader in analytics';
y=prxchange('s/(.)(.)/\u\1\2/',-1,x);
run;
art297
Opal | Level 21

You will fail the question if you provide the response you chose as correct. Carefully compare the test phrase with the output from each suggestion.

 

Art, CEO, AnalystFinder.com

 

gamotte
Rhodochrosite | Level 12

Hello,

 

In your example, H and E in "tHE" are both upper case. I guess this is a mistake and so is the remainder of

the example string.

 

data test;
    x='sas is the leader in analytics';

    upcase_this=1;

    do i=1 to length(x);
        if anyalpha(substr(x,i))=1 then do;
            if upcase_this then substr(x,i,1)=upcase(substr(x,i,1));
            upcase_this=1-upcase_this;
        end;
    end;
    put x;
run;
ballardw
Super User

My first response would be: What are the actual RULES involved in the manipulation? One single literal example => Type the desired response. Why bother attempting to GUESS an algorithm without rules to implement?

 

Example deduce the rule behind:

 

1   => 3

2   => 3

3   => 5

4   => 4

5   => 4

 

So what would be the result for 6, 7 and 8?

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 14 replies
  • 949 views
  • 7 likes
  • 9 in conversation