BookmarkSubscribeRSS Feed
rohithrao
Calcite | Level 5

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

11 REPLIES 11
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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?

rohithrao
Calcite | Level 5

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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!

rohithrao
Calcite | Level 5

still spaces are removed...

Ksharp
Super User

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

TarunKumar
Pyrite | Level 9

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;

KachiM
Rhodochrosite | Level 12

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;

data_null__
Jade | Level 19

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.

1-7-2015 7-35-32 AM.png

FriedEgg
SAS Employee

12       

13         data _null_;

14         text='Hello World';

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

16         call pokelong(text, addrlong(r[1]));

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!

data_null__
Jade | Level 19

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;

16         call pokelong(text, addrlong(r[1]));

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?

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 11 replies
  • 7079 views
  • 3 likes
  • 8 in conversation