Hi,
I have a variable 'name' like this -
data have;
input name $30. ;
datalines;
aaaaa+
bbb+aa+
dd+cc
fff+aa+dd+
gg+d
;
run;
I need the last character in the string to be removed only if it is a '+'. Some observations don't end in a '+' and those should be left as is. Also the '+' in other positions in the variable should be left as is, only if it is in the last position, it needs to be remove. How do I do this? Any advice is appreciated!
This is how I want it to look:
Name
aaaaa
bbb+aa
dd+cc
fff+aa+dd
gg+d
Thanks!
Alternative:
data want;
set have;
if substr(name,length(name),1) = '+' then name = substr(name,1,length(name)-1);
run;
data have;
input name $30. ;
a=compress(name,' ','ka');
datalines;
aaaaa+
bbb+aa+
dd+cc
fff+aa+dd+
gg+d
;
run;
by using compress function with keep only alphabetics we can remove '+' from the given data
data have;
input name $30. ;
if substr(reverse(strip(name)),1,1)='+' then a=reverse(substr(reverse(strip(name)),2));
else a=name;
datalines;
aaaaa+
bbb+aa+
dd+cc
fff+aa+dd+
gg+d
;
run;
Alternative:
data want;
set have;
if substr(name,length(name),1) = '+' then name = substr(name,1,length(name)-1);
run;
data have; input name $30. ; want=prxchange('s/\++$//',1,strip(name)); datalines; aaaaa+ bbb+aa+ dd+cc fff+aa+dd+ gg+d ; run;
Thanks everyone! it worked!
Hi,
I'm wondering how it works can you please explain
prxchange function.
Thanks,
Rajesh
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.