Hi,
I'm a SAS newbie. I was trying out something to reverse the string. I know there is SAS function REVERSE but I want to write my own code for fun 🙂
here is my code
data try;
name="Bellevue";
length revname $ 20;
retain revname;
do i= length(name) to 1 by -1;
revname=catx(revname, strip(substr(name, i, 1)));
output;
end;
run;
but if you run the code, I'm not getting string though I've used retain statement. Whats going wrong?
Several items to note:
Good points from @Astounding. Regarding trailing blanks, I don't think they would be desired as leading blanks in variable REVNAME (and they wouldn't occur as such with the current approach). But if they were desired, one could create them easily with a final revname=right(revname);.
Retain is for keeping values as you move down a column or through the data set. Since you're not stepping through a traditional data set and operating on the same line the Retain function isn't required.
Here is another approach, using a temporary array
data try;
length name revname $ 20;
array c{100} $1 _temporary_;
do name = "Bellevue", "stressed";
call missing(of c{*});
do i= 1 to length(name);
c{dim(c)-i+1} = char(name,i);
end;
revname=cats(of c{*});
output;
end;
drop i;
run;
proc print noobs data=try; run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.