BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10
data nnn;
set sashelp.class;
length rev_string $ 25;
do i=1 to length(rev_string);
rev_string=substr(Name,length(Name)-i,1);
end;drop i ;
proc print;run;

how reverse string (Name) where i did wrong in the class dataset

9 REPLIES 9
PeterClemmensen
Tourmaline | Level 20

Is this an assignment? Otherwise, why not use the Reverse Function?

BrahmanandaRao
Lapis Lazuli | Level 10

Yes Peter 

 I am trying instead of REVERSE function

PaigeMiller
Diamond | Level 26

@BrahmanandaRao wrote:

Yes Peter 

 I am trying instead of REVERSE function


Why write your own code instead of the REVERSE function?

--
Paige Miller
ger15xxhcker
Quartz | Level 8

The DO loop you wrote is incorrect. To reverse a string, you can use the SAS function REVERSE(). You can use it, like this:
rev_string = reverse(Name);

Astounding
PROC Star

Your DO loop replaces REV_STRING with a single character each time through the loop ... not what is needed.  Do you need help in fixing that?

 

What about blanks?  For example, this code contains 7 trailing blanks in STRING:

 

length string $ 10;
string = 'ABC';
rev_string = reverse(string);

Using the REVERSE function, the result would contain 7 leading blanks followed by "CBA".  Are you attempting to replicate that result, or are you intending to ignore the blanks and get "CBA" followed by 7 trailing blanks?

PaigeMiller
Diamond | Level 26

@Astounding wrote:

Your DO loop replaces REV_STRING with a single character each time through the loop ... not what is needed.  Do you need help in fixing that?

 

What about blanks?  For example, this code contains 7 trailing blanks in STRING:

 

length string $ 10;
string = 'ABC';
rev_string = reverse(string);

Using the REVERSE function, the result would contain 7 leading blanks followed by "CBA".  Are you attempting to replicate that result, or are you intending to ignore the blanks and get "CBA" followed by 7 trailing blanks?


Either of the "problems" caused by spaces in the REVERSE function are easy to fix. Easier than writing your own code.

--
Paige Miller
Astounding
PROC Star
You're absolutely right, the REVERSE function would be easier and more accurate. I'm assuming that this is a homework assignment and the instructor is trying to make that exact point: functions are thoroughly tested and debugged and easier to use when compared to "do it yourself" code.
Kurt_Bremser
Super User

Use two index variables: one to count down from length(name) to 1, the other starting at 1 and incrementing in the loop. Then use SUBSTR on the left and right side of the assignment (left for rev_string, right for name).

PeterClemmensen
Tourmaline | Level 20

Also, an opportunity to use the rarely used character format $REVERS Format .

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 1494 views
  • 3 likes
  • 6 in conversation