Help using Base SAS procedures

Creating Last First Mi from a single variable

Accepted Solution Solved
Reply
Super Contributor
Posts: 268
Accepted Solution

Creating Last First Mi from a single variable

The data I have (in one field):

Name 

Doe,  John  M. 

The data I want (in 3 fields):

Last     First     Mi

Doe     John     M


Accepted Solutions
Solution
‎06-11-2015 04:01 PM
Regular Contributor
Posts: 180

Re: Creating Last First Mi from a single variable

Very simplistic but works if there are no composed names, prefixes, more than one initial, etc...:

data have;

  name='Doe, John M';

  last=scan(name,1);

  first=scan(name,2);

  Mi=scan(name,3);

run;

CTorres

View solution in original post


All Replies
Solution
‎06-11-2015 04:01 PM
Regular Contributor
Posts: 180

Re: Creating Last First Mi from a single variable

Very simplistic but works if there are no composed names, prefixes, more than one initial, etc...:

data have;

  name='Doe, John M';

  last=scan(name,1);

  first=scan(name,2);

  Mi=scan(name,3);

run;

CTorres

Super Contributor
Posts: 268

Re: Creating Last First Mi from a single variable

Simplistic works for me!  Thanks.

Super User
Posts: 10,497

Re: Creating Last First Mi from a single variable

Beware of two word last names such as Le Blank, Van Dorp and such.

You might want to run a word count to see if you have more than 3 words in the field to bring possible problems to your attention.

Frequent Contributor
Posts: 130

Re: Creating Last First Mi from a single variable

I'm not sure how dynamic you need the code to be, whether your Name field is consistent or not with the length of the Middle Initial and whether there are more than two names for the first name, but this code will help with your example you gave.

data have;

input name $25.;

datalines;

Doe, John M.

;

run;

data want;

set have;

format Last First $15. FirstMi $20. Mi $1;

Last=scan(Name,1,",");

FirstMi=scan(Name,2,",");

First=strip(scan(FirstMi,1," "));

Mi=compress(scan(FirstMi,2," "),".");

drop FirstMi;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 213 views
  • 0 likes
  • 4 in conversation