DATA Step, Macro, Functions and more

Creating a list of IPs from the range of IPS

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 86
Accepted Solution

Creating a list of IPs from the range of IPS

Hi Experts

I need to do investigation of web log data and need to see the IP address origination. For a referral match, I searched over internet and found a range of IPs(not the actual IP list) . Below the Sample of From IP to IP

From                    To                           Count

1.6.0.01.7.255.255131072
1.22.0.01.23.255.255131072
1.38.0.01.39.255.255131072
1.186.0.01.186.255.25565536
1.187.0.01.187.255.25565536


In the first record, the list would be as I could understand

1.6.0.0 , 1.6.0.1 .........1.6.0.255, 1.6.1.0, 1.6.1.1 .....1.6.1.255 and so on. Every octect needs to be countered till the "TO" IP

How can this be achieved considering the volume of ranges exisiting

Any help or alternative would be appreciated

Thanks in Advance

Regards


Accepted Solutions
Solution
‎08-08-2014 04:10 AM
Super User
Super User
Posts: 7,401

Re: Creating a list of IPs from the range of IPS

Hi,

Well, I would suggest you need 4 nested do loops, something along the lines of:

data want;  

     do I=1 to 1;

          do j=6,7,22,23,38,39,186,187;                /* updated for all */

               do k=0 to 255;

                    do l=0 to 255;

                         ip=compress(put(I,best.)||'.'||put(j,best.)||'.'||put(k,best.)||'.'||put(l,best.));

                         output;

                    end;

               end;

          end;

     end;

run;

View solution in original post


All Replies
Solution
‎08-08-2014 04:10 AM
Super User
Super User
Posts: 7,401

Re: Creating a list of IPs from the range of IPS

Hi,

Well, I would suggest you need 4 nested do loops, something along the lines of:

data want;  

     do I=1 to 1;

          do j=6,7,22,23,38,39,186,187;                /* updated for all */

               do k=0 to 255;

                    do l=0 to 255;

                         ip=compress(put(I,best.)||'.'||put(j,best.)||'.'||put(k,best.)||'.'||put(l,best.));

                         output;

                    end;

               end;

          end;

     end;

run;

Frequent Contributor
Posts: 86

Re: Creating a list of IPs from the range of IPS

Many Many Thanks

This solution worked for me. I just made the looping dynamic and the code worked like charm. Many Thanks

Valued Guide
Posts: 3,208

Re: Creating a list of IPs from the range of IPS

IPv4 addressing is based on 4 bytes. They are based on bit/byte processing.  Testing via mask's is easy with BAND BXOR

What you could do is accessing your problem just like that. Convert the decimal reprentation as it was in the IP protocol 4 bytes (char).  SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition (byte)

The testing of ranges will just become a bit-check non zero after some band-masking.

---->-- ja karman --<-----
Super User
Posts: 6,938

Re: Creating a list of IPs from the range of IPS

This is a more complete approach:

%let start=1.6.0.0;

%let end=1.7.255.255;

data ip_ad (keep=ip_address);

start_ad = 0;

do i = 1 to 4;

  i1 = input(scan("&start",i,'.'),3.);

  start_ad = start_ad + i1 * 256**(4-i);

end;

end_ad = 0;

do i = 1 to 4;

  i1 = input(scan("&end",i,'.'),3.);

  end_ad = end_ad + i1 * 256**(4-i);

end;

length ip_address $ 15;

do address = start_ad to end_ad;

  ip_address = ' ';

  char_ad = put(address,hex8.);

  do i = 0 to 3;

    ip_address = compress(ip_address !! put(input(substr(char_ad,i*2+1,2),hex2.),best3.));

    if i ne 3 then ip_address = trim(ip_address) !! '.';

  end;

  output;

end;

run;

RW9's example would have problems with

start=1.7.0.0

end=2.6.255.255

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Frequent Contributor
Posts: 86

Re: Creating a list of IPs from the range of IPS

Thanks guys. The logics have really helped me build my ip address repository

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 207 views
  • 6 likes
  • 4 in conversation