Solved
Contributor
Posts: 72

# Reverse any string without using function (Reverse)

String= 'abcdefghijklmnopqrstuvwxyz';

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

## Re: Reverse any string without using function (Reverse)

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

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

## Re: Reverse any string without using function (Reverse)

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

Posts: 3,852

## 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)

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,778

## Re: Reverse any string without using function (Reverse)

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: 72

## Re: Reverse any string without using function (Reverse)

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.