DATA Step, Macro, Functions and more

how to reverse a string with out using reverse function

Reply
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
Super User
Posts: 7,401

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: 17,829

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
Super User
Posts: 7,401

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: 9,681

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

Contributor
Posts: 74

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

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;

Respected Advisor
Posts: 3,777

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.

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

Trusted Advisor
Posts: 1,300

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;

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!

Respected Advisor
Posts: 3,777

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;

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?

Ask a Question
Discussion stats
  • 11 replies
  • 1295 views
  • 2 likes
  • 8 in conversation