BookmarkSubscribeRSS Feed
Anandkvn
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 @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.)

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
Quartz | Level 8
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 2024

Innovate_SAS_Blue.png

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. 

Register now!

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 252 views
  • 5 likes
  • 5 in conversation