DATA Step, Macro, Functions and more

Cut the variable

Accepted Solution Solved
Reply
Contributor
Posts: 47
Accepted Solution

Cut the variable

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

 

 


Accepted Solutions
Solution
‎11-17-2015 09:48 AM
Trusted Advisor
Posts: 1,137

Re: Cut the variable

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


All Replies
Super User
Super User
Posts: 7,942

Re: Cut the variable

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.

Super Contributor
Posts: 275

Re: Cut the variable

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
;
Solution
‎11-17-2015 09:48 AM
Trusted Advisor
Posts: 1,137

Re: Cut the variable

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
🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

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