BookmarkSubscribeRSS Feed
shravanisreeyan
Fluorite | Level 6

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

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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;

novinosrin
Tourmaline | Level 20


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;
PaigeMiller
Diamond | Level 26

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.

 

 

--
Paige Miller
Ksharp
Super User
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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1534 views
  • 3 likes
  • 4 in conversation