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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 632 views
  • 3 likes
  • 4 in conversation