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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 3250 views
  • 1 like
  • 4 in conversation