- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Alternative:
data want;
set have;
if substr(name,length(name),1) = '+' then name = substr(name,1,length(name)-1);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Alternative:
data want;
set have;
if substr(name,length(name),1) = '+' then name = substr(name,1,length(name)-1);
run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
data have; input name $30. ; want=prxchange('s/\++$//',1,strip(name)); datalines; aaaaa+ bbb+aa+ dd+cc fff+aa+dd+ gg+d ; run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks everyone! it worked!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
I'm wondering how it works can you please explain
prxchange function.
Thanks,
Rajesh