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;

Catch up on SAS Innovate 2026

Nearly 200 sessions are now available on demand with the SAS Innovate Digital Pass.

Explore 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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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