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

Hi folks.  I'm looking for a method to create a variable of the string up until the last blank space in the string.

 

nm1 =  'SAN ANTONIO CA';

nm2 =  'NEW SAN ANTONIO CA'

 

So for nm1 I'd want to return only 'SAN ANTONIO'

So for nm2 I'd want to return only 'NEW SAN ANTONIO'

 

Any thoughts on how to do this?  Basically the requirement is to return everything prior to the final blank space in the string? But the under of blank spaces may vary.

 

Any help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

Hi @buechler66  Please see if this helps

 

data have;
length nm $50;
nm =  'SAN ANTONIO CA';
output;
nm =  'NEW SAN ANTONIO CA';
output;
run;

data want;
 set have;
 length want $30;
 want=substr(nm,1,findc(trim(nm),' ','b')-1);
run;

Or something fancy

 

 want=prxchange('s/(.*)\s.+$/$1/',-1, trim(nm));

 

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

Hi @buechler66  Please see if this helps

 

data have;
length nm $50;
nm =  'SAN ANTONIO CA';
output;
nm =  'NEW SAN ANTONIO CA';
output;
run;

data want;
 set have;
 length want $30;
 want=substr(nm,1,findc(trim(nm),' ','b')-1);
run;

Or something fancy

 

 want=prxchange('s/(.*)\s.+$/$1/',-1, trim(nm));

 

buechler66
Barite | Level 11

I appreciate you taking the time to help.  Greatly!

Tom
Super User Tom
Super User

It is easy to find the value after the last space

scan(name,-1,' ')

Once you have that you can figure out how many characters to chop off.

susbtrn(name,1,length(name)-length(scan(name,-1,' '))

But what do want to do when there are no embedded spaces?

name='LONDON';

Do you want an empty string?  Or do you want to remove nothing?

buechler66
Barite | Level 11
Ty for posting this. I appreciate ur time.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 1526 views
  • 1 like
  • 3 in conversation