## Perform reverse function on a fixed number of character

Solved
Occasional Contributor
Posts: 14

# Perform reverse function on a fixed number of character

Hi

I need help performing a reverse function in the data below. The approach is to perform a reverse on the first 9, leave the next 9 strings as it is, perform a reverse on the next 9, skip the next 9 and it goes on till the  length of String is reached. While the solution below solves this particular problem, it's not dynamic. Any help will be appreciated. Thanks

Data Have =1234567890abcdefghijklmnopqr

Data Want=9876543210abcdefghrqponmlkjirs

data help;

String='1234567890abcdefghijklmnopqr';

length StringR \$50;

StringR=reverse(Substr(String,1,9))||substr(String,10,18)||reverse(Substr(String,19,27));

run;

Accepted Solutions
Solution
‎12-14-2014 11:39 PM
Posts: 4,742

## Re: Perform reverse function on a fixed number of character

This has nothing to do with the reverse function Xia or I proposed but simply that a blank is used as a delimiter in our sample code. So variable Have got already only string "123". Just change the input statement like below and things will work - with a blank treated like any other character when reversing sub-strings.

ods listing;

data _null_;

infile datalines truncover;

input have \$90.;

length want \$90;

want=prxchange('s/(.)(.)?(.)?(.)?(.)?(.)?(.)?(.)?(.)?(.{0,9})/\9\8\7\6\5\4\3\2\1\${10}/oi',-1,trim(Have));

put have=;

put want= /;

datalines;

123456789abcdefghi

123456789abcdefghi123456789abcdefghi12

123456789abcdefghi123456789abcde

123456789abcdefghi123456789

123 456789abcdefghi123456789

;

run;

All Replies
Super User
Posts: 23,776

## Re: Perform reverse function on a fixed number of character

Sounds like a home work question so only a few hints:

1. Can you always assume a length that's divisible by 9

2. Look at either a do loop after you calculate the length of the string or a do while loop

3. You can take each 9 section of a string and operate on it and concatenate it until the loop ends.

Occasional Contributor
Posts: 14

## Re: Perform reverse function on a fixed number of character

No, this is definitely not a homework question. Though it sounds like one

Super User
Posts: 23,776

## Re: Perform reverse function on a fixed number of character

And regarding Q1?

Super Contributor
Posts: 578

## Re: Perform reverse function on a fixed number of character

might have create a loop with a counter.  Say the length of the string is 80.  You would loop floor(80/9) times starting at 0.  You could build some temp variables to hold the start/stop points that you need to work on.

format tmpStr \$200;

tmpStr="";

do i = 0 to MaxLoops;

if i mod(i,2) = 0 then tmpStr=cats(tmpStr,reverse(substring(string,i*9,i*9+9)));

else tmpStr=cats(tmpStr,substring(string,i*9,i*9+9));

end;

Posts: 4,742

## Re: Perform reverse function on a fixed number of character

Not 100% sure if I understand your requirement correctly. Below code will reverse position 1-9, leave 10-18 unchanged, and then repeat the process until the end of the source string.

If this "skip" in your description means to remove position 28-36 then below code would need amendment.

ods listing;

data _null_;

infile datalines truncover;

input have :\$90.;

length want \$90;

want=prxchange('s/(.)(.)?(.)?(.)?(.)?(.)?(.)?(.)?(.)?(.{0,9})/\9\8\7\6\5\4\3\2\1\${10}/oi',-1,trim(Have));

put have=;

put want= /;

datalines;

123456789abcdefghi

123456789abcdefghi123456789abcdefghi12

123456789abcdefghi123456789abcde

123456789abcdefghi123456789

run;

Result:

have=123456789abcdefghi

want=987654321abcdefghi

have=123456789abcdefghi123456789abcdefghi12

want=987654321abcdefghi987654321abcdefghi21

have=123456789abcdefghi123456789abcde

want=987654321abcdefghi987654321abcde

have=123456789abcdefghi123456789

want=987654321abcdefghi987654321

Super User
Posts: 10,787

## Re: Perform reverse function on a fixed number of character

```data want;
infile datalines truncover;
input have :\$90.;
length want \$ 90;
do i=1 to length(have) by 9;
n+1;
if mod(n,2)=0 then want=cats(want,substr(have,i,9));
else want=cats(want,reverse(substr(have,i,9)));
end;
datalines;
1234567890abcdefghijklmnopqr
123456789abcdefghi
123456789abcdefghi123456789abcdefghi12
123456789abcdefghi123456789abcde
123456789abcdefghi123456789
run;

```

Xia Keshan

Occasional Contributor
Posts: 14

## Re: Perform reverse function on a fixed number of character

Thanks Xia, Your code works except that if I insert a blank character such as 123 4567890abcdefghijklmnopqr, it resolves to 321

Posts: 4,742

## Re: Perform reverse function on a fixed number of character

Below expression will do:

want=prxchange('s/(.)(.)?(.)?(.)?(.)?(.)?(.)?(.)?(.)?(.{0,9})/\9\8\7\6\5\4\3\2\1\${10}/oi',-1,trim(Have));

Occasional Contributor
Posts: 14

## Re: Perform reverse function on a fixed number of character

Yes, the code works except that if I have something like 123 4567890abcdefghijklmnopqr, it resolves to 321

Solution
‎12-14-2014 11:39 PM
Posts: 4,742

## Re: Perform reverse function on a fixed number of character

This has nothing to do with the reverse function Xia or I proposed but simply that a blank is used as a delimiter in our sample code. So variable Have got already only string "123". Just change the input statement like below and things will work - with a blank treated like any other character when reversing sub-strings.

ods listing;

data _null_;

infile datalines truncover;

input have \$90.;

length want \$90;

want=prxchange('s/(.)(.)?(.)?(.)?(.)?(.)?(.)?(.)?(.)?(.{0,9})/\9\8\7\6\5\4\3\2\1\${10}/oi',-1,trim(Have));

put have=;

put want= /;

datalines;

123456789abcdefghi

123456789abcdefghi123456789abcdefghi12

123456789abcdefghi123456789abcde

123456789abcdefghi123456789

123 456789abcdefghi123456789

;

run;

Occasional Contributor
Posts: 14

## Re: Perform reverse function on a fixed number of character

Thanks Patrick for the information. Also, thanks to Xia. I finally got it to yeild my desire output by simply changing the input statement Patrick suggested.

🔒 This topic is solved and locked.