Reputation: 11
I have a SAS data set with observations like this:
Abatucci Pierre*L'entrée au château*1000*1*75*91
Agneessens Edouard*Jeune femme*6000*1*40*32*5
where * is the field separator. How can I create a new variable that has First_name_letter+" "+Surname like this:
Abatucci Pierre ----> P Abatucci
Agneessens Eduard ----> E Agneessens
Upvotes: 0
Views: 415
Reputation: 11755
The cleanest way to do this is with the Perl regular expression functionality that is built into SAS:
data names;
set dataset;
name = prxchange('s/(\w)\w* (\w+)/$2 $1/', -1, name);
run;
(I'm assuming that the *s in your question are actually field separators, because your other recent question asked how to deal with that.)
The expression (\w)\w* (\w+)
causes the first letter of the first word of name
and the second word of name
to be put into variables $1
and $2
. The rest of the command tells SAS to substitute $2 $1
for this variable.
Upvotes: 2
Reputation: 31
try this.
data name;
infile cards dlm='*';
length name name2 $35 entree $35;
input name entree calories portion cost weight;
name2=substr(scan(name,2,' '),1,1) ||' '||scan(name,1,' ');
datalines;
Abatucci Pierre*L'entrée au château*1000*1*75*91
Agneessens Edouard*Jeune femme*6000*1*40*32*5
run;
Upvotes: 2