how to reverse a string with out using reverse function , by using do loop?
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?
Homework
it is not working in log window it mesge that numeric varibles are converted in to char and char to numeric....
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!
still spaces are removed...
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
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;
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;
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.
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!
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 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.