BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Andrew15
Fluorite | Level 6

Hi. I needed to reverse string without using "reverse' function,i did but got a result without spaces. How can i apply them? The code is below:

 

/* 4. Reverse the string without using "reverse" function. */
data b1;
length reversed $200.;
var1='Reverse the string without using "reverse" function.';
erk=length(var1);
do i=length(var1) to 1 by -1;
reversed=strip(reversed) || substr(var1, i, 1 ) ;

end;
drop erk i;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The STRIP() function is removing both any LEADING space and any TRAILING spaces.

 

Just keep track of where you are putting the character.

do i=1 to length(var1);
   substr(reversed,length(var1)-i+1,1)=substr(var1, i, 1 ) ;
end;
4601   data b1;
4602   length reversed $200.;
4603   var1='Reverse the string without using "reverse" function.';
4604   erk=length(var1);
4605   do i=1 to length(var1);
4606     substr(reversed,length(var1)-i+1,1)=substr(var1, i, 1 ) ;
4607   end;
4608   drop erk i;
4609   put (_character_) (/);
4610   run;


.noitcnuf "esrever" gnisu tuohtiw gnirts eht esreveR
Reverse the string without using "reverse" function.
NOTE: The data set WORK.B1 has 1 observations and 2 variables.

If you want to really mimic the way REVERSE() works when passed a variable name then use VLENGTH() instead of LENGTH() when calculating the location in the reversed variable.  That will preserve the trailing spaces that are stored in the variable.

 substr(reversed,Vlength(var1)-i+1,1)=substr(var1, i, 1 ) ;

View solution in original post

8 REPLIES 8
Tom
Super User Tom
Super User

The STRIP() function is removing both any LEADING space and any TRAILING spaces.

 

Just keep track of where you are putting the character.

do i=1 to length(var1);
   substr(reversed,length(var1)-i+1,1)=substr(var1, i, 1 ) ;
end;
4601   data b1;
4602   length reversed $200.;
4603   var1='Reverse the string without using "reverse" function.';
4604   erk=length(var1);
4605   do i=1 to length(var1);
4606     substr(reversed,length(var1)-i+1,1)=substr(var1, i, 1 ) ;
4607   end;
4608   drop erk i;
4609   put (_character_) (/);
4610   run;


.noitcnuf "esrever" gnisu tuohtiw gnirts eht esreveR
Reverse the string without using "reverse" function.
NOTE: The data set WORK.B1 has 1 observations and 2 variables.

If you want to really mimic the way REVERSE() works when passed a variable name then use VLENGTH() instead of LENGTH() when calculating the location in the reversed variable.  That will preserve the trailing spaces that are stored in the variable.

 substr(reversed,Vlength(var1)-i+1,1)=substr(var1, i, 1 ) ;
Andrew15
Fluorite | Level 6
Thank you!
yabwon
Onyx | Level 15
data b1;
  length reversed $200.;
  var1='Reverse the string without using "reverse" function. ĄŻŚŹĘĆŃÓŁ';
  j=1;
  do i = klength(var1) to 1 by -1;
    
    x=ksubstr(var1, i, 1); /* K...() functions are for UTF-8 symbols */
    substr(reversed, j, length(x)) = x; 
    j+length(x);
  end;
run;

proc print; run;
_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Oligolas
Barite | Level 11
DATA b1;
   var1='Reverse the string without using "reverse" function.';
   reversed=put(var1,$REVERS32767.);
RUN;
________________________

- Cheers -

PaigeMiller
Diamond | Level 26

Is this a homework assignment? This is the second time this question has been asked in a short period of time.

 

SAS has provided the exact tool you want to reverse a string. That is the REVERSE function. It has been thoroughly tested and debugged. Why would you say that this tool can't be used? Why do you want a more complicated method that has been less thoroughly tested and debugged?

--
Paige Miller
Andrew15
Fluorite | Level 6

Yes it's homework task,and i am not allowed to use Reverse tool.

PaigeMiller
Diamond | Level 26

@Andrew15 wrote:

Yes it's homework task,and i am not allowed to use Reverse tool.


Well then I think your instructor has created a stupid problem. There are plenty of other things he could have asked you to program that don't have a built-in tool. One of the benefits of SAS (and most programming languages) is that commonly needed tools are provide so you don't have to create your own. I hope that if you ever have a need for this outside of this class, you will use the REVERSE function.

--
Paige Miller
ballardw
Super User

If interested I can describe how I did this once upon a time in an assembler class by manipulating the output register buffers...

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 8 replies
  • 597 views
  • 4 likes
  • 6 in conversation