Reverse any string without using function (Reverse)

Accepted Solution Solved
Reply
Contributor
Posts: 64
Accepted Solution

Reverse any string without using function (Reverse)

String= 'abcdefghijklmnopqrstuvwxyz';


Accepted Solutions
Solution
‎04-16-2014 01:31 PM
PROC Star
Posts: 1,322

Re: Reverse any string without using function (Reverse)

Posted in reply to DR_Majeti

Um. why?

15   data _null_;
16     String='abcd';
17     length Reverse $4;
18     do i=1 to length(String);
19       substr(Reverse,i,1)=substr(String,length(String)+1-i,1);
20     end;
21     put (String Reverse)(=);
22   run;

String=abcd Reverse=dcba

View solution in original post


All Replies
Solution
‎04-16-2014 01:31 PM
PROC Star
Posts: 1,322

Re: Reverse any string without using function (Reverse)

Posted in reply to DR_Majeti

Um. why?

15   data _null_;
16     String='abcd';
17     length Reverse $4;
18     do i=1 to length(String);
19       substr(Reverse,i,1)=substr(String,length(String)+1-i,1);
20     end;
21     put (String Reverse)(=);
22   run;

String=abcd Reverse=dcba

Respected Advisor
Posts: 3,799

Re: Reverse any string without using function (Reverse)

Quentin wrote:

Um. why?

That's the same question I have.  There is a FORMAT that might be used.

data _null_;
   String=
'abcdefghijklmnopqrstuvwxyz';
  
put _all_;
   string=putc(string,
'REVERJ',length(string));
   put _all_;
  
run;

15         data _null_;
16            String= 'abcdefghijklmnopqrstuvwxyz';
17            put _all_;
18            string=putc(string,'REVERJ',length(string));
19            put _all_;
20            run;

String=abcdefghijklmnopqrstuvwxyz _ERROR_=
0 _N_=1
String=zyxwvutsrqponmlkjihgfedcba _ERROR_=
0 _N_=1
Regular Contributor
Posts: 217

Re: Reverse any string without using function (Reverse)

Posted in reply to DR_Majeti

This can be refined, but this works without using the Reverse function.

data _null_;

String= 'abcdefghijklmnopqrstuvwxyz';

stringlen = length(string);

length reversestring $ 26;  /* ideally this would be set to be the same length of stringlen */

length onevalue $ 1;

    do i=1 to stringlen;

       if i=1 then do;

           reversestring = substr(string,stringlen,1);

       end;

       else do;

          if i ne stringlen then do;

           position = stringlen - i;

           onevalue = substr(string,position,1);

           reversestring = cats(reversestring,onevalue);

          end;

       end;

    end;

  put reversestring=;

run;

reversestring=zxwvutsrqponmlkjihgfedcba

Super User
Posts: 10,020

Re: Reverse any string without using function (Reverse)

Posted in reply to DR_Majeti

It looks like a homework.

data _null_;
     String='abcd';
     length Reverse $4;
     do i=length(String) to 1 by -1;
       reverse=cats(reverse,char(string,i));
     end;
     put (String Reverse)(=);
   run;

Xia Keshan

Contributor
Posts: 64

Re: Reverse any string without using function (Reverse)

Posted in reply to DR_Majeti

Thank you all...

It is a task given to try...unable to get the dynamic code.

And working on how does PDV acts.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 1004 views
  • 8 likes
  • 5 in conversation