hi, can you help me to reverse a string without using REVERSE function?
String 'i just want to play a Game'
Why would you want to avoid using the exact function that does EXACTLY what you want?
Hi,
you can do that for fun, I currently see 2 ways.
data test;
length string $50;
string='i just want to play a Game ';
run;
*use the $reversw. format;
data want;
set test;
string2=put(string,$REVERS32767.);
run;
*Build your reverse string char by char starting from the end;
data want;
set test;
length string2 $50;
retain string2;
string2='';
j=0;
do i=lengthn(string) to 1 by -1;
j+1;
string2=substrn(string2,1,j)||substrn(string,i,1);
put string2=;
end;
run;
- Cheers -
This program does it:
%let str = %str(i just want to play a Game);
%let len = %length(&str);
data _null_;
length str_in str_out $ &len;
str_in = "&str";
do i = 1 to &len;
substr(str_out, i, 1) = substr(str_in, &len + 1 - i, 1);
end;
put str_in= str_out=;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.