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;
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 ) ;
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 ) ;
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;
DATA b1;
var1='Reverse the string without using "reverse" function.';
reversed=put(var1,$REVERS32767.);
RUN;
- Cheers -
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?
Yes it's homework task,and i am not allowed to use Reverse tool.
@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.
If interested I can describe how I did this once upon a time in an assembler class by manipulating the output register buffers...
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.