Do Loop Concatenate

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Do Loop Concatenate

Hi All,

As a fairly new user, I'm looking for a bit of assistance on the below. Any help is greatly appreciated!

In the below code I have four phone fields that I'm looking to cleanse if they have a series of 7 consecutive numbers in them.

So my thinking is that the first do loop of 1 to 4 will cycle through the numbers and the second do loop I'm trying to get to replace any matching number with 0's, i.e. if any of the 4 phone_ fields contain at least 7 consecutive 1111111, 2222222 etc then it'll be replaced by 10 zeros. The phone_ field is formatted to text and I'm struggling with getting {j} to calculate to and concatenate and be enclosed with single quotes for the index statement to work....

Cheers,

Scott

do i = 1 to 4;

do j = 1 to 9;

  consec = '||{j}||{j}||{j}||{j}||{j}||{j}||{j}';

  if index(phone_{i},consec) gt 0 then do;

  phone_{i}='0000000000';

  end;

end;

end;


Accepted Solutions
Solution
‎11-19-2014 10:58 PM
Respected Advisor
Posts: 3,156

Re: Do Loop Concatenate

Another way to do it is to leverage Perl Regular expressions:

data have;

     input telno $10.;

     if prxmatch('/1{7}|2{7}|3{7}|4{7}|5{7}|6{7}|7{7}|8{7}|9{7}/o', telno) then

           new_tel='0000000000';

     else new_tel=telno;

     cards;

1234567899

1111111523

1234444444

5263548245

1255555555

2222222222

;

quit;


Haikuo

View solution in original post


All Replies
Contributor
Posts: 62

Re: Do Loop Concatenate

Please use proc format and apply it to your dataset.

Super User
Posts: 11,144

Re: Do Loop Concatenate

Short fix:

consec = cats(j,j,j,j,j,j,j);

Just in case you have 7 consecutive 0: do j = 0 to 9;

I've had phone numbers that were XXX0000000 where the area code was valid but the remainder were 0.

Super User
Super User
Posts: 6,851

Re: Do Loop Concatenate

Use the PUT() function to convert a number like J to a character string.

Use the REPEAT() function to generate multiple copies of the same string.

To get 7 copies of the digit represented by the number J you could use:

consec=repeat(put(j,1.),7-1) ;

That said why not just use these functions to generate the code and then paste the generated strings into your program and eliminate the inner loop.

if index(phone_(i),'0000000' )

or index(phone_(i),'1111111' )

or index(phone_(i),'2222222' )

or index(phone_(i),'3333333' )

or index(phone_(i),'4444444' )

or index(phone_(i),'5555555' )

or index(phone_(i),'6666666' )

or index(phone_(i),'7777777' )

or index(phone_(i),'8888888' )

or index(phone_(i),'9999999' )

then phone_(i)='0000000';

;

Solution
‎11-19-2014 10:58 PM
Respected Advisor
Posts: 3,156

Re: Do Loop Concatenate

Another way to do it is to leverage Perl Regular expressions:

data have;

     input telno $10.;

     if prxmatch('/1{7}|2{7}|3{7}|4{7}|5{7}|6{7}|7{7}|8{7}|9{7}/o', telno) then

           new_tel='0000000000';

     else new_tel=telno;

     cards;

1234567899

1111111523

1234444444

5263548245

1255555555

2222222222

;

quit;


Haikuo

Occasional Contributor
Posts: 6

Re: Do Loop Concatenate

Massive thanks to all! All correct answers but decided to go with Hai.kuo as had never used prxmatch like that before so good for the experimentation!

Cheers!

🔒 This topic is solved and locked.

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

Discussion stats
  • 5 replies
  • 353 views
  • 6 likes
  • 5 in conversation