## Creating a list of IPs from the range of IPS

Solved
Frequent Contributor
Posts: 86

# 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.0 1.7.255.255 131072 1.22.0.0 1.23.255.255 131072 1.38.0.0 1.39.255.255 131072 1.186.0.0 1.186.255.255 65536 1.187.0.0 1.187.255.255 65536

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

Regards

Accepted Solutions
Solution
‎08-08-2014 04:10 AM
Super User
Posts: 9,599

## 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;

All Replies
Solution
‎08-08-2014 04:10 AM
Super User
Posts: 9,599

## 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

Posts: 3,215

## 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: 10,280

## 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;

do i = 1 to 4;

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

end;

do i = 1 to 4;

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

end;

do i = 0 to 3;

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
How to convert datasets to data steps
How to post code
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 and locked.