DATA Step, Macro, Functions and more

Getting values in one column

Accepted Solution Solved
Reply
Contributor
Posts: 65
Accepted Solution

Getting values in one column

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!


Accepted Solutions
Solution
‎09-27-2011 03:41 PM
PROC Star
Posts: 7,360

Getting values in one column

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


All Replies
Solution
‎09-27-2011 03:41 PM
PROC Star
Posts: 7,360

Getting values in one column

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

;

Super User
Super User
Posts: 6,499

Getting values in one column

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;

Valued Guide
Posts: 2,174

Getting values in one column

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?

Contributor
Posts: 65

Getting values in one column

@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.

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

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