Help using Base SAS procedures

Do loop within text variable

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

Do loop within text variable

Hi guys

Wonder if you can help. I'm writing some code to check whether an IP address is within a range of IP addresses.

I have used the following code, however I rekon that the reference to counter "i" is not being resolved and is been taken as a literal i.

the IP address within the table is a string variable not numeric.

Data foo;

set bar;

     do i = 97 to 126;

     If originatingIPad = 'xx.xxx.xx.&i.' then keep=1;

     end;

Run;

Data foobar(drop=keep);

set foo;

where keep=1;

run;

Thanks in advance for any assistance offered.

Jamie


Accepted Solutions
Solution
‎07-26-2011 11:04 AM
PROC Star
Posts: 7,363

Do loop within text variable

Jamie,

That isn't a macro variable.  Try:

Data foo;

  set bar;

     do i = 97 to 126;

     If originatingIPad = 'xx.xxx.xx.'||strip(i) then keep=1;

     end;

Run;

View solution in original post


All Replies
Solution
‎07-26-2011 11:04 AM
PROC Star
Posts: 7,363

Do loop within text variable

Jamie,

That isn't a macro variable.  Try:

Data foo;

  set bar;

     do i = 97 to 126;

     If originatingIPad = 'xx.xxx.xx.'||strip(i) then keep=1;

     end;

Run;

Respected Advisor
Posts: 3,777

Do loop within text variable

Do you need to test each value?  If you convert to numeric you can test using a range.

data ip;

   input ip:$16.;

   cards;

   000.000.000.123

   000.000.000.127

;;;;

run;

proc print;

   where 96 le input(scan(ip,-1,'.'),f3.) le 126;

   run;

Contributor
Posts: 24

Do loop within text variable

Thanks guys :-)

PROC Star
Posts: 7,363

Do loop within text variable

I'm glad we were able to help but, the more I think about it, aren't you really going to need something like:

data ip;

length originatingIPad $16;

input originatingIPad;

keep=ifn(97<=scan(originatingIPad,-1,'.')<=126,1,.);

cards;

10.0.0.0

10.0.0.1

10.255.255.96

12.12.12.12

172.16.0.97

172.31.255.255

191.191.191.126

192.168.0.0

192.168.255.127

192.168.255.125

255.255.255.254

255.255.255.255

;

run;

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 145 views
  • 3 likes
  • 3 in conversation