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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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