BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Pritish
Quartz | Level 8

Hi All,

I know this is a very stupid question to ask, but I am bit confused with the following:

How can I get the First Name and Middle Name in the same column and Last name in a different column?

As I don't have double space before the last name so I can't use '&' in my INPUT statement.

Data Sample;

Input First_name $ Last_Name;

Cards;

Nadal Anthony Samuel

John Beth Goodrich

;

Run;

Result:

First_Name               Last_Name

Winston Anthony      Samuel

John Beth                 Goodrich

Thanks for your help!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I'm sure there are a number of ways to do it.  If you ALWAYS have first and middle names then you could use something like:

Data Sample (drop=Middle_name);

  length First_name $30.;

  informat First_name Middle_name Last_Name $15.;

  input First_name Middle_name Last_Name;

  First_name=catx(' ',First_name,Middle_name);

  cards;

Nadal Anthony Samuel

John Beth Goodrich

;

View solution in original post

4 REPLIES 4
art297
Opal | Level 21

I'm sure there are a number of ways to do it.  If you ALWAYS have first and middle names then you could use something like:

Data Sample (drop=Middle_name);

  length First_name $30.;

  informat First_name Middle_name Last_Name $15.;

  input First_name Middle_name Last_Name;

  First_name=catx(' ',First_name,Middle_name);

  cards;

Nadal Anthony Samuel

John Beth Goodrich

;

Tom
Super User Tom
Super User

This is actually a difficult problem to handle properly as some last names include spaces.

data sample;

  input @ ;

  length first_name $50 last_name $50 ;

   last_name = scan(_infile_,-1,' ');

   first_name = substr(_infile_,1,length(_infile_)-length(last_name));

cards;

Nadal Anthony Samuel

John Beth Goodrich

;

Run;

Peter_C
Rhodochrosite | Level 12

is there always just one middle name?

how often might a "last name" be more than one word (not counting double-barrelled surnames separated with a ( - ) hyphen )?

I have two middle names. Should both follow my first name?

Pritish
Quartz | Level 8

@Art and @Tom - Thanks for sharing the knowledge.

@Peter, Well it depends on the business rules and requirements. I was writing some code and came up with this and was not able to think clearly on the solution. So that's the reason I came up with this question.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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