BookmarkSubscribeRSS Feed
kindbe17
Fluorite | Level 6

hi, can you help me to reverse a string without using REVERSE function?

 

String 'i just want to play a Game' 

3 REPLIES 3
PaigeMiller
Diamond | Level 26

Why would you want to avoid using the exact function that does EXACTLY what you want?

--
Paige Miller
Oligolas
Barite | Level 11

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 -

drdivago
Calcite | Level 5

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;

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon Kicks Off on June 11!

Watch the live Hackathon Kickoff to get all the essential information about the SAS Hackathon—including how to join, how to participate, and expert tips for success.

YouTube LinkedIn

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
  • 3 replies
  • 970 views
  • 5 likes
  • 4 in conversation