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;
This should correct your word counting:
data _null_;
full_name = 'Mr James Headey-Greaves';
word_count = countw(full_name,,'AL');
put _all_;
run;
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: