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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.