BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ahhh
Obsidian | Level 7

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!

 

 

1 ACCEPTED SOLUTION
9 REPLIES 9
rajeshalwayswel
Pyrite | Level 9


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

ahhh
Obsidian | Level 7
Thanks! But, I would still like to keep the '+' in the middle. Only if the string ends with a '+' , only that '+' needs to be removed. Other '+'s should be left as is...
rajeshalwayswel
Pyrite | Level 9


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;

Ksharp
Super User

data have;
input name $30. ;
want=prxchange('s/\++$//',1,strip(name));
datalines;
aaaaa+
bbb+aa+
dd+cc
fff+aa+dd+
gg+d
;
run;

ahhh
Obsidian | Level 7

Thanks everyone! it worked!

rajeshalwayswel
Pyrite | Level 9

Hi,

 

I'm wondering how it works can you please explain 

prxchange function.

 Thanks,

Rajesh

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 15312 views
  • 0 likes
  • 4 in conversation