- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I appreciate you taking the time to help. Greatly!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content