Help using Base SAS procedures

Seperate names

Reply
Contributor
Posts: 74

Seperate names

Hi,

I have a situation,where i have a variable NAME
eg:
NAME
----------
JOHN C MAT,MD
SARA ATIO,MD

I would like to seperate this name as Fname,MIDname,Lname
so my output look like
Fname MIDname Fname
---------------------------------------------------
JOHN C MAT,MD
SARA ATIO,MD

Please let me know,if you have a solution for this.

thanks,
Ren
SAS Super FREQ
Posts: 8,740

Re: Seperate names

Hi:
There are a number of character string functions -- the simple ones, like SCAN and SUBSTRING and FIND and INDEX and the more complex ones, like the Perl Regular Expression which would allow you to break up a string into chunks.

There's a simple example at the end of this post. If you don't have regular names (maybe 1 middle name, maybe 2 middle names, possible space before MD, maybe not, maybe 2 credentials), then you may want to go down the Perl Regular Expression road. There have been many postings in the Forum on splitting up text strings.

As you can see from the log results of this program (with the Big Bird, Yellow) results, the SCAN approach only lends itself to very regular data.

cynthia
[pre]
data break_it_up;
length name $30;
infile datalines dsd;
input name $;
first = scan(name,1,' ');
middle = scan(name,2,' ');
last = scan(name,3,' ');
suffix = scan(name,4,', ');
if last = ' ' then do;
** takes care of no middle name;
last = middle;
middle = ' ';
end;
put "*************************************************";
put (_all_) (=);
return;
datalines;
"Kermit The Frog,Esq."
"Oscar The Grouch, GC"
"Miss Piggy"
"Big Bird, Yellow"
;
run;


LOG RESULTS from PUT statements:
*************************************************
name=Kermit The Frog,Esq. first=Kermit middle=The last=Frog,Esq. suffix=Esq.
*************************************************
name=Oscar The Grouch, GC first=Oscar middle=The last=Grouch, suffix=GC
*************************************************
name=Miss Piggy first=Miss middle= last=Piggy suffix=
*************************************************
name=Big Bird, Yellow first=Big middle=Bird, last=Yellow suffix=
NOTE: The data set WORK.BREAK_IT_UP has 4 observations and 5 variables.

[/pre]
Contributor
Posts: 74

Re: Seperate names

Thanks Cynthia
Ask a Question
Discussion stats
  • 2 replies
  • 129 views
  • 0 likes
  • 2 in conversation