Hi Guys,
name='George W. Bush'
required output:
Fname lname
George W. Bush
Hi @BrahmanandaRao; 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.)
Cheers
Markus
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 full_name
. The -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.
data have;
name='George W. Bush';
call scan(name,-1,p,l,' ');
firstname=substr(name,1,p-1);
lastname=substr(name,p);
run;
proc print;run;
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.
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.