Hi Experts,
I need to separate first name and last name which do not have any delimiter specified.
I have the following data set:
data sample;
input name : $20.;
datalines;
gurpreetkaur
anujmadan
dhirajdhingra
mehakbhasin
sahibakhurana
;
run;
I need a dataset from the above dataset with ouput as:
first name | last name |
gurpreet | kaur |
anuj | madan |
dhiraj | dhingra |
mehak | bhasin |
sahiba | khurana |
how should I get the above data set??
Thanks !!
Gurpreet Kaur
If there is no rule or pattern you can describe that identifies where the word boundaries are then there is no way to program it in SAS or any other computer language.
The only way I suspect this would work is if your software had access to an appropriate database of surnames and first names and you were able to do name matches. This is definitely not available in Base SAS.
Hi mate,
For this small table you can use multiple IFs and substr function to separate what you need.
But if your table is very large and there is no patterns, then you need a new way to get this table
I agree with Kiwi.
But for this small table you can do this:
%macro name(name,pos,len,pos1,len1);
data want_&name(where=(last_name ne ""));
set sample;
if substr(name,1,1) = "&name." then do;
last_name = substr(name,&pos. ,&len.);
first_name = substr(name,&pos1.,&len1.);
end;
run;
%mend;
%name(g,9,6 ,1,8);
%name(a,5,6 ,1,4);
%name(d,7,10,1,9);
%name(m,6,10,1,5);
%name(s,7,10,1,6);
And then append.
Hope this helps
Agree with everyone above. Even something as simple as capital letter for each first and last name would help. Anything to determine the start position of each name element (including middle initials if there are any).
So if you altered the data in any way to make them all lowercase, then maybe you have the information you need, otherwise, you will need to request a new file with the appropriate delimiters.
It is possible that what you presented was not exactly what the interviewer asked or the data example you provided had some difference.
A very minor change such as the name parts starting with a capital letter would make it possible.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.