DATA Step, Macro, Functions and more

Infinite Loop Question

Occasional Contributor
Posts: 5

Infinite Loop Question

Hi, I have one question on infinite loop. There are two programs, which are shown below. 


Why does program A work fine but program B generates infinite loop? thanks. 


Program A:

filename qtr1 ('c:\sasuser\month1.dat''c:\sasuser\month2.dat'


data work.firstqtr;

infile qtr1;

input Flight $ Origin $ Dest $

Date : date9. RevCargo : comma15.;



Program B:


data work.quarter;

do Month = 9, 10, 11;


!!compress(put(Month,2.)!!".dat",' ');

do until (lastobs);

infile temp filevar=nextfile end=lastobs;

input Flight $ Origin $ Dest $ Date : date9.

RevCargo : comma15.;





Occasional Contributor
Posts: 8

Re: Infinite Loop Question

!!compress(put(Month,2.)!!".dat",' '); 

Don't you mean


You used exclamation marks. Why do you take the compress function?

Valued Guide
Posts: 2,191

Re: Infinite Loop Question

More than one issue:
On windows I think filevar needs to provide a variable holding a single path (and a filename that could hold global chars*? ).
Is the default length of NEXTFILE wide enough to hold the required path\file name ?
I expect you need to reset the value of LASTOBS to zero as it will be retained.
Super User
Super User
Posts: 8,279

Re: Infinite Loop Question

[ Edited ]

Is this a test?


SAS normally stops a data step when it reads past the end of the input data set or text file.

Run this little example to see what I mean.


data _null_;
   put 'BEFORE INPUT' _n_= ;
   input  ;
   put 'AFTER INPUT' _n_ = ;


In your second data step the conditions on your DO loops prevent it from reading past the end of the data. So on iteration 1 it does DO MONTH=9,10,11 and then on iteration 2 it does it again, ad infinitum.  Add a STOP statement before the RUN statement.



Ask a Question
Discussion stats
  • 3 replies
  • 1 like
  • 4 in conversation