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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.