BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lerdem
Quartz | Level 8

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

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag

View solution in original post

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

slchen
Lapis Lazuli | Level 10

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
;
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 2599 views
  • 1 like
  • 4 in conversation