BookmarkSubscribeRSS Feed
BrahmanandaRao
Lapis Lazuli | Level 10

Hi Guys,

name='George W. Bush'

required output:

Fname                      lname

George W.                Bush

4 REPLIES 4
MarkusWeick
Barite | Level 11

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

Please keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
himself
Pyrite | Level 9
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.

Ksharp
Super User
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;
ballardw
Super User

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: 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
  • 4 replies
  • 1353 views
  • 5 likes
  • 5 in conversation