BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Anmolkhandelwal
Obsidian | Level 7

Trying to get reverse of these records.

 

Input dataset:

 

data temp;
input list $50.;
cards;
hellothere
thankyou
goodbye
welcome
;
run;

Tried something like this:

data test;
set temp;
l = length(list);
format list1 $50.
list1 = '';
do i= 1 to l;
list1 = cats(list1,substr(list,i,-1));

end;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Panagiotis
SAS Employee

I think this should work.

 

data test;
	set temp;
	length newword $100; /*create a new column with a length of 100*/
	newword=""; /*Start the value of the new column as a blank*/
	l = length(list); /*find the length of the word*/
	do i=l to 1 by -1; /*start from the last character and go to the first*/
		letter=char(list,i); /*get the last letter, go down by one each loop*/
		newword=cats(newword,letter); /*concatenate every letter throughout the loop*/
	end;
run;

 

 

View solution in original post

8 REPLIES 8
Astounding
PROC Star

What exactly are you trying to do?

 

A beginning string of "hellothere" followed by 40 blanks, reversed would normally be 40 blanks followed by "erehtolleh".  Is that your goal, or are you trying to get "erehtolleh" followed by 40 blanks?

Panagiotis
SAS Employee

I think this should work.

 

data test;
	set temp;
	length newword $100; /*create a new column with a length of 100*/
	newword=""; /*Start the value of the new column as a blank*/
	l = length(list); /*find the length of the word*/
	do i=l to 1 by -1; /*start from the last character and go to the first*/
		letter=char(list,i); /*get the last letter, go down by one each loop*/
		newword=cats(newword,letter); /*concatenate every letter throughout the loop*/
	end;
run;

 

 

Panagiotis
SAS Employee

I didn't think about what @Astounding brought up. Mine program just reverses the word.

ErikLund_Jensen
Rhodochrosite | Level 12

Hi @Anmolkhandelwal  and @Panagiotis 

 

I wonder why anybody would want to do it without using the reverse function, unless it is a challenge: Write an algoritm that preserves blanks, not only leading and trailing, but also embedded in the string.

 

I think that excludes CAT-functions and also trim + concatenation and calls for the substr(left of) function:

 


data test;
	text = '      this is a      string ';
	revtext = text;
	imax = length(text||'.')-1;
	do i = imax to 1 by -1;
	 	ri = imax - i + 1;
		substr(revtext,ri,1) = substr(text,i,1);
	end;

	utext= '/' || text || '/';
	urevtext = '/' || revtext || '/';
	put utext;
	put urevtext;
run;

 

 

novinosrin
Tourmaline | Level 20

Great point sir @ErikLund_Jensen 

"I wonder why anybody would want to do it without using the reverse function, unless it is a challenge "

 

I would share an experience from a failed interview of mine The interviewer had the attitude that he knew everything and quizzed me on the syntax, which I failed to answer. Basically that event was just an ego event. I do like competition and being competitive but didn't quite expect that in an interview to show off "I know more and am better than you" 🙂  Funnily enough, that person called his mate to bully me saying " have i been told whether it's a SAS interview"

 

Of course no offense to OP and I am sure here in a forum is perhaps truly a challenge. Nonetheless, I was expressing my opinion. 

ballardw
Super User

@ErikLund_Jensen wrote:

Hi @Anmolkhandelwal  and @Panagiotis 

 

I wonder why anybody would want to do it without using the reverse function, unless it is a challenge: Write an algoritm that preserves blanks, not only leading and trailing, but also embedded in the string.

 

 

 

 


In my case this was the first assignment in an assembler programming class.

 

Instructor assigned reversing a numeric value expecting loops and modular arithmetic. I used buffer operations that would reverse any string and got extra credit for a more generic approach.

FreelanceReinh
Jade | Level 19

Hi @Anmolkhandelwal,

 

If only the REVERSE function is prohibited, why not take the rare opportunity to use the $REVERSw. or $REVERJw. format? 🙂

data want;
set temp;
list=put(list,$revers50.); /* or $reverj50. to preserve the leading (formerly trailing) blanks */
run;
novinosrin
Tourmaline | Level 20

Some fun with APP and Arrays combined

 

data temp;
input list $50.;
cards;
hellothere
thankyou
goodbye
welcome
;
run;

data want;
 set temp;
 length want $50;
 array t(50)$1 ;
 call pokelong(list,addrlong(t(1)),50);
 array u t50-t1;
 want= cats(of u(*));
 drop t:;
run;

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
  • 1325 views
  • 7 likes
  • 7 in conversation