Fluorite | Level 6

## Reverse

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
Super User

## Re: Reverse

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 ) ;``
8 REPLIES 8
Super User

## Re: Reverse

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 ) ;``
Fluorite | Level 6

Thank you!
Onyx | Level 15

## Re: Reverse

``````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

Barite | Level 11

## Re: Reverse

``````DATA b1;
var1='Reverse the string without using "reverse" function.';
reversed=put(var1,\$REVERS32767.);
RUN;``````
________________________

- Cheers -

Diamond | Level 26

## Re: Reverse

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
Fluorite | Level 6

## Re: Reverse

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

Diamond | Level 26

## Re: Reverse

@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
Super User

## Re: Reverse

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

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