BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Mohan_Reddy
Obsidian | Level 7
I have data set like below...

data stansys;
infile datalines;
input id name&$24. sal;
datalines;
101 Richard Rose           5000
102 Yao Chen Hoo           6000
103 Asha Garg Bette Long   7000
104 Jason Blue             9000
105 Susan Robert Stewart   8000
;
run;


Through this dataset i want output dataset with seperating as First name and Middle name and last name...

So please give a programme for this !!!
1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

@Mohan_Reddy wrote:

 

Here First name="Richard" and Last name="Rose"


if another records have two separate names in string you will take first name,Middle name1,Middle name2,Last name......No issues

 


And how many middle names do you expect in your data?

 

I also like to point you to https://communities.sas.com/t5/New-SAS-User/How-to-take-patient-name-and-split-into-first-name-and-l..., especially the message from PaigeMiller, showing that extracting name-parts is not that easy.

 

Try:

data want;
   set stansys;
   length first middle1 middle2 last $ 40;
   array parts[*] first middle1 middle2 last;

   do i = 1 to countw(name);
      if i = countw(name) and i < dim(parts) then do;
         parts[dim(parts)] = scan(name, i);
      end;
      else do;
         parts[i] = scan(name, i);
      end;
   end;

   drop i;
run;

If you want all middle-names in a single variable, i would use a regular expression.

View solution in original post

7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

In a name like "Asha Garg Bette Long", what do you want the first, middle and last name to be?

Mohan_Reddy
Obsidian | Level 7

"Asha Garg Bette Long" through this You will take Middle name1="Garg" and Middle name2="Bette Like no issues.....

andreas_lds
Jade | Level 19

Taking the first row of your data, both "Richard" and "Rose" could be first names. So please define which words are first name, middle name and last name.

Mohan_Reddy
Obsidian | Level 7

 

Here First name="Richard" and Last name="Rose"


if another records have two separate names in string you will take first name,Middle name1,Middle name2,Last name......No issues

 

andreas_lds
Jade | Level 19

@Mohan_Reddy wrote:

 

Here First name="Richard" and Last name="Rose"


if another records have two separate names in string you will take first name,Middle name1,Middle name2,Last name......No issues

 


And how many middle names do you expect in your data?

 

I also like to point you to https://communities.sas.com/t5/New-SAS-User/How-to-take-patient-name-and-split-into-first-name-and-l..., especially the message from PaigeMiller, showing that extracting name-parts is not that easy.

 

Try:

data want;
   set stansys;
   length first middle1 middle2 last $ 40;
   array parts[*] first middle1 middle2 last;

   do i = 1 to countw(name);
      if i = countw(name) and i < dim(parts) then do;
         parts[dim(parts)] = scan(name, i);
      end;
      else do;
         parts[i] = scan(name, i);
      end;
   end;

   drop i;
run;

If you want all middle-names in a single variable, i would use a regular expression.

Mohan_Reddy
Obsidian | Level 7
In First row First Name ="Richard" and Last Name="Rose"...

In 2 and 3 and 5 Rows only you will Take First Name,Middle Name1,Middle Name2,Last Name like that i want output.....
ballardw
Super User

@Mohan_Reddy wrote:
In First row First Name ="Richard" and Last Name="Rose"...

In 2 and 3 and 5 Rows only you will Take First Name,Middle Name1,Middle Name2,Last Name like that i want output.....

You may also have to consider names such as "de la Cruz" or "van Dyke" which are not unlikely last names if you have enough.

Also do you have any titles such as Dr. Ms. Esq. in the data?

Of things like Junior, "the third", II and similar ilk?

 

If at all practical I would go the source and see if they can create a separate clearly delimited file with each of first., last and middle names.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 7 replies
  • 4441 views
  • 0 likes
  • 4 in conversation