BookmarkSubscribeRSS Feed
Justin9
Obsidian | Level 7

Can someone tell me how I would change my code to accommodate for double-barrelled names? I am creating a new variable called 'Middle_Initial' and trying to get the first letter of the first middle name (if someone has a middle name, it  will be always the third word, after title and forename), but my code is counting double-barrelled names as two words instead of one.

 

For example, with Ms Anne-Marie Beasley, my code is counting four words instead of three. As a result, my created 'Middle_Initial' variable is showing "B" instead of being blank (as Anne-Marie is supposed to just be one word for the forename).

 

Another example is Mr James Headey-Greaves. My countw function is counting four words instead of three (as Headey-Greaves is the surname and Headey is not a middle name), so my created 'Middle_Initial' variable is showing "H" instead of being blank.

 

Please can someone help to suggest a fix to my code?

data final_checks;
   set testing;
   Title=scan(Name,1,1,' ');
   Forename=scan(Name,1,2,' ');
   countofwords=countw(Name); /*Issue with double-barrelled names not counting as one word*/
   if countofwords>3 then Middle_Initial=substr((scan(Name,3,' ')),1,1); /*Issue with double-barrelled names not counting as one word*/
   Surname=scan(Name,-1,' ');
run;

 

1 REPLY 1
SASKiwi
PROC Star

This should correct your word counting:

data _null_;
  full_name = 'Mr James Headey-Greaves';
  word_count = countw(full_name,,'AL');
  put _all_;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

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

View all other training opportunities.

Discussion stats
  • 1 reply
  • 242 views
  • 0 likes
  • 2 in conversation