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 !!!
@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.
In a name like "Asha Garg Bette Long", what do you want the first, middle and last name to be?
"Asha Garg Bette Long" through this You will take Middle name1="Garg" and Middle name2="Bette Like no issues.....
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.
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
@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 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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.