data have;
input a$30.;
cards;
st john school
st xavier school
st little flower school
;
run;
data want
john
xavier
little flower
Can i get code for this?many thanks
data have;
input a$30. ;
cards;
st john school
st xavier school
st little flower school
;
run;
data want;
set have;
length need $100;
do _n_=2 to countw(a,' ')-1;
need=catx(' ',need,scan(a,_n_));
end;
run;
data have;
input a$30. ;
cards;
st john school
st xavier school
st little flower school
;
run;
data want;
set have;
length need $100;
_n_=anyspace(strip(a));
need=substr(a,_n_,anyspace(strip(a),-99)-_n_);
run;
Hello, @shravanisreeyan, maybe you could please provide more realistic example, so we can solve the real problem?
What do you want to have as a result if a name does not have a middle name? Does that ever happen in your data?
What do you want as a result if a last name has two "words", such as Stan Van Gundy, in this case Van Gundy is the last name, and no middle name. Does that ever happen in your data?
Parsing names can require some pretty complicated rules, if you are working with real world data.
data have; input a$30. ; cards; st john school st xavier school st little flower school ; run; data want; set have; want=prxchange('s/^\w+|\w+$//',-1,strip(a)); run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.