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;
... View more