Since those with (0, >1) hyphens are to be left alone, I check for that first. After that, just check for a comma and position of the hyphen relative to the comma. If there is no comma, check for the position of the hyphen relative to the space. I've only reassigned the values when necessary.
data have;
infile cards truncover;
input Name $300.;
cards;
Jane Doe
Smith-John-F
Jones-Anderson, Dan
Kate, Simons-Hunt
Parker-Parks Ashley
;
run;
data want;
set have;
if countc(name,'-') eq 1 then do;
if countc(name,',') eq 1 and countc(scan(name,1,','),'-') eq 0
then name = catx(', ',scan(name,2,','),scan(name,1,','));
else if countc(name,',') eq 0 and countc(scan(name,2,' '),'-') eq 0
then name = catx(' ',scan(name,2,' '),scan(name,1,' '));
end;
run;
... View more