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.

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