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
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
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
Hi Reeza
This is interview question
can we solve it
@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.
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
Hi Unison
Thank you very much brilliant code
Sightly simpler:
if mod(I,2) then substr(X,I,1)= upcase(char(X,I));
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
Than You sir support
your coding skills are par excellence
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.
data test;
x='sas is the leader in analytics';
y=prxchange('s/(.)(.)/\u\1\2/',-1,x);
run;
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
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;
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?
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!
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.