BookmarkSubscribeRSS Feed
tsureshinvites
Obsidian | Level 7

Hi,

Please assist me to write algorithm for the below task.

 

Input raw data:

String = "Suresh Kumar thangaraj"

 

Output required:

New_String = "hseruS ramuK jaragnaht"

 

But my output;

My output;

"jaragnaht ramuK hseruS";

 

My Code:

data test;
length New_string $30;
string = "Suresh Kumar thangaraj";
L = length(string);
do i = 1 to L;
substr(New_string,i,1) = substr(string,L+1-i,1);
end;
run;

 

Regards,

Suresh Kumar

1 REPLY 1
mkeintz
PROC Star

YOU want to keep word order, but reverse all the letters in each word.

 

YOu need to know:

  1. How many words are in STRING  (take a look at the COUNTW function).
  2. How to loop over each word one at a time  (consider DO W=1 to number_of_words_in_STRING).
  3. How to extract one word at a time from STRING (look at the SCAN function)
  4. How to reverse a sequence of text in the word  (look at the REVERSE function)
  5. How to concatenate (with space separation) each reversed word, one at a time into NEW_STRING  (look at the CATX function).

 

It's all about the journey.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------