I have a tabel as below:
id phone
1 0112976555559, 2955940123, 4072948756, 4071453259
2 4072564138
3 3051485236, 4078596321, 4072483516
4 1254862, 4075847456
I need only first 2 phones like this
id phone
1 0112976555559, 2955940123
2 4072564138
3 3051485236, 4078596321
4 1254862, 4075847456
Here is an alternative approach similar to @RW9
data want;
length new $100;
set have;
do i=1 to 2 ;
new=catx(',',new,scan(phone,i,','));
end;
run;
Hi,
The code:
phone=catx(', ',scan(phone,1,','),scan(phone,2,','));
Should od the trick, or:
phone=substr(phone,1,findc(phone,',',index(phone,',')+1));
Note, haven't tested the second, might need a tinker or two to work.
However the question should really be, why do you have multiple data elements in one variable. This is not a good way to do things, keep you underlying data separated out for easy processing, then concatenate at report time.
data want; set have; array phone{4} $20.; do i=1 to 4; phone{i}=scan(phone,i,','); end; run;
You can then do anything you want with variables phone1-phone4.
Here is regular expression.
data have;
infile cards missover;
input id phone &$200.;
_phone=prxchange('s/(^\d+,\d+).*/$1/',-1,phone);
cards;
1 0112976555559,2955940123,4072948756,4071453259
2 4072564138
3 3051485236,4078596321,4072483516
4 1254862,4075847456
;
Here is an alternative approach similar to @RW9
data want;
length new $100;
set have;
do i=1 to 2 ;
new=catx(',',new,scan(phone,i,','));
end;
run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.