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

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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 277 views
  • 0 likes
  • 2 in conversation