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.
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));
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));
I appreciate you taking the time to help. Greatly!
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?
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.