## how to reverse a string with out using reverse function

Occasional Contributor
Posts: 7

# how to reverse a string with out using reverse function

how to reverse a string with out using reverse function , by using do loop?

Super User
Posts: 9,599

## Re: how to reverse a string with out using reverse function

Well, you could do:

do I=length(variable) to 1 by -1;

new_var=strip(new_var)||substr(variable,i,1);

end;

The question would be why when there is a perfectly good function for it?

Super User
Posts: 23,733

## Re: how to reverse a string with out using reverse function

Homework

Occasional Contributor
Posts: 7

## Re: how to reverse a string with out using reverse function

it is not working in log window it mesge that numeric varibles are converted in to char and char to numeric....

Super User
Posts: 9,599

## Re: how to reverse a string with out using reverse function

Hi,

Not sure why that is, this works:

data temp;

test="Hello World";

length new_var \$200;

do i=length(test) to 1 by -1;

new_var=strip(new_var)||substr(test,i,1);

end;

run;

Although I note that after looking at the output, spaces between words are removed.  Hadn't thought about that as just typed the code in as an example.  Use the function!

Occasional Contributor
Posts: 7

## Re: how to reverse a string with out using reverse function

still spaces are removed...

Super User
Posts: 10,784

## Re: how to reverse a string with out using reverse function

Do  you want keep these trailing blanks ?

```data temp;
test="Hello World      ";
length new_var \$200;
array x{200} \$ 1 _temporary_;
n=length(test);
do i=n to 1 by -1 ;
x{n-i+1}=char(test,i);
end;
new_var=cat(of x{*});
call missing(of x{*});
drop  i n ;
run;

```

Xia Keshan

Message was edited by: xia keshan

Frequent Contributor
Posts: 80

## Re: how to reverse a string with out using reverse function

data test;

my_name = 'Tarun Kumar';

length my_name_new\$20.;

do i = 1 to length(my_name);

substr(my_name_new,i,1) = substr(my_name,length(my_name)+1-i,1);

end;

put (my_name my_name_new) (=);

run;

Super Contributor
Posts: 324

## Re: how to reverse a string with out using reverse function

Reversing the String IN PLACE.

data _null_;

t = 'Hello World SAS Communities';

length C \$1;

len = length(t);

j = len;

do i = 1 to len while(i < j);

C = substr(t, i, 1);

substr(t, i, 1) = substr(t, j, 1);

substr(t, j, 1) = C;

j +- 1;

end;

put t = ;

run;

Posts: 3,852

## Re: how to reverse a string with out using reverse function

```rohithrao wrote:

how to reverse a string with out using reverse function , by using do loop?
```

You read it using the \$REVERJ INFORMAT of course.

Posts: 1,318

## Re: how to reverse a string with out using reverse function

12

13         data _null_;

14         text='Hello World';

15         array r[11] \$ 1 c11-c1;

17         reverse=cat(of c1-c11);

18         put reverse;

19         run;

dlroW olleH

Message was edited by: FriedEgg, well noted @DN, I updated to reflect your comment, forgot that little tidbit!

Posts: 3,852

## Re: how to reverse a string with out using reverse function

```FriedEgg wrote:

12
13         data _null_;
14         text='Hello World';
15         array r[11] \$ 1 c11 c10 c9 c8 c7 c6 c5 c4 c3 c2 c1;
17         reverse=cat(of c1-c11);
18         put reverse;
19         run;

dlroW olleH
```

Did you know that an enumerated variable list can be written ascending or descending, as in C11-C1?

Discussion stats
• 11 replies
• 2118 views
• 2 likes
• 8 in conversation