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;

 

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 203 views
  • 5 likes
  • 4 in conversation