loopssasdo-loops

Process Each Row of Dataset During Each Iteration in SAS


I have a dataset in SAS and need to process each row of the dataset in a do loop during each iteration.

Right now, I have the following:

data output (drop=i);
    set work.input;
    
    do i = 1 to 1000; 
        /*Code to process on each iteration */
    end; 
run;

when this runs, it will process the first row 1000 times and then the second row 1000 times. Which normally probably would not be an issue, however I am using this as part of Monte Carlo simulation so the outcome of each row will affect the probability of the next row. Therefore I need to process each row of the dataset on each iteration.

I have also tried this:

data output (drop=i);
    
    do i = 1 to 1000;
        do until(eof_data);
            set work.input end=eof_data;
            
            /*Code to process on each row*/

            output;

        end; 
    end;
run;

This does not seem to work either (I tried it on only 2 iterations just to test it and placed a variable called sim_num = i just to see how it was working and it only went to sim_num = 1 and stopped).

I thought about maybe an array of some sort, but I am less familiar with SAS arrays, and really SAS in general (I generally work in R and Python and this is not an issue with those languages).

I appreciate the help!


Solution

  • I suspect this is an https://xyproblem.info/.

    But to loop over a dataset repeatedly just use the POINT= option of the SET statement.

    data want;
       * setup stuff ;
       do p=1 to nobs;
         set have point=p nobs=nobs ;
         * stuff to do in the loop ;
       end;
       * stuff to do after the loop;
       stop;
    run;
    

    Note that the SET statement with the POINT= option will never read past the end of the dataset. So if you do not have another SET statement or INFILE statement that will read past the end of its input your data step will loop forever if you do not include a STOP statement.