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;
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;
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?
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;
I didn't think about what @Astounding brought up. Mine program just reverses the word.
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;
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.
@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.
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;
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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.