Hi @Anandkvn; have a look here: Separate Full Name to FirstName and LastName - SAS Support Communities
(And next time please look for existing solutions before asking already answered questions.)
data output; set YourDataset; /* Replace "YourDataset" with the name of your actual dataset */ Fname = scan(full_name, 1); /* Assuming the full name is stored in a variable called "full_name" */ Lname = scan(full_name, -1); /* Extracts the last word in the full name */ /* Output the formatted names */ put "Fname: " Fname; put "Lname: " Lname; run;
In this code, you need to replace "YourDataset" with the actual name of your dataset containing the full names. The
SCAN function is used to extract the first name (
Fname) and last name (
Lname) from the variable
-1 as the second argument of
SCAN tells SAS to extract the last word in the string. Finally, the
put statements are used to display the formatted output in the SAS log.
Make sure to adjust the code according to your specific dataset and variable names.
Now, what do you expect for a "first" and "last" name from these names
Sun Flower Moon Child
John Richard II Van Opdorp
Maria Garcia Y Mendoza
Smith, John David Junior
Junior Childs Junior
There are rules for parsing names that are cultural and almost any simple one or two line programming solution won't "work" for all of them and may not work for large numbers.
If the question is "what are the first two words out of this list of 3 words" that is one thing.
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.