## How to use a Do loop when the code is a mixed between Proc/IML and other SAS/STAT

I have a code that contains parts from SAS/STAT and parts from Proc/IML.

The code censors data then calculate the area.

The censoring is done first for 500 observations then goes up to 5000 observations with increment of 500.

I need help in how to write the loop in this case. I am using SAS 9.4

Thanks a lot

1 ACCEPTED SOLUTION

Accepted Solutions

## Re: How to use a Do loop when the code is a mixed between Proc/IML and other SAS/STAT

That's fine. As I said, you can modify the program for other situations such as you describe.

``````N = 10154;
do case = 1 to 3;
if case=1 then
obsIdx = 501:N;
else if case=2 then
obsIdx = 1:(N-500);
else if case=3 then do;
middle = round(N/2);
midIdx = (middle-249):(middle+250); /* or -250 and +249 */
obsIdx = setdif(1:N, midIdx);
end;
...
end;``````
6 REPLIES 6

## Re: How to use a Do loop when the code is a mixed between Proc/IML and other SAS/STAT

I do not fully understand your problem, but do the various samples build on each other? Can you read in the final 5000 observations and then analyze the first 500, the first 1000, the first 1500, etc?  (or maybe you are analyzing (1:500, 501:1000, ...? It's not clear.)

If so, here is the basic structure for subsetting the data. I assume you want to analyze 1:500, 1:1000, 1:1500, etc, but the program can be modified to  other situations:

``````
proc iml;
start TrapIntegral(x,y);
N = nrow(x);
dx = x[2:N] - x[1:N-1];
meanY = ( y[2:N] + y[1:N-1] )/2;
return( dx` * meanY );
finish;

grid = T( do(-5+0.002, 5, 0.002) );
density = pdf("Normal", grid);     /* N(0,1) density on [-5, 5] */
N = nrow(density);
print N;

sizes = T( do(500, N, 500) );
Area = j(nrow(sizes), 1, .);
do i = 1 to nrow(sizes);
obsIdx = 1:sizes[i]; /* or sizes[i-1]:sizes[i] ? */
x = grid[obsIdx];    /* subset the data */
y = density[obsIdx];
Area[i] = TrapIntegral(x, y);
end;

print sizes Area;``````

## Re: How to use a Do loop when the code is a mixed between Proc/IML and other SAS/STAT

My issue is that I need to read and estimate the density for the data using three types of censoring

case 1: censoring from the begging, for this case I will not take the first 500 observations, I start my data from obs=501 till the end of the data.

case 2:   censoring the last 500 observation, so my data will start from obs=1 and end with 10154-500

Case 3: censoring from the middle so I will read all the data except the 500 observations from the middle.

For each of these data sets I compute the kernel.

My problems is that I have to do it again for 1000, 1500,...,5000.

I have done it by adjusting the numbers manually and run the code, but this is very primitive and I want something rigorous.

Thank you

## Re: How to use a Do loop when the code is a mixed between Proc/IML and other SAS/STAT

That's fine. As I said, you can modify the program for other situations such as you describe.

``````N = 10154;
do case = 1 to 3;
if case=1 then
obsIdx = 501:N;
else if case=2 then
obsIdx = 1:(N-500);
else if case=3 then do;
middle = round(N/2);
midIdx = (middle-249):(middle+250); /* or -250 and +249 */
obsIdx = setdif(1:N, midIdx);
end;
...
end;``````

## Re: How to use a Do loop when the code is a mixed between Proc/IML and other SAS/STAT

You will need a macro %DO loop.

Something like this:

``````%macro dothis;
%do start = 1 %to 4501 %by 500;
proc whatever data=mydata(firstobs=&start obs=500);
...
run;
proc iml;
use mydata(firstobs=&start obs=500);
read var _num_ /* or a list of variable names */  into x;
...
quit;
%end;
%mend;

%dothis``````

--
Paige Miller  Ksharp
Super User

## Re: How to use a Do loop when the code is a mixed between Proc/IML and other SAS/STAT

use SUBMIT in IML.

proc iml;

.......

submit ;

proc reg data=class;

...........

endsub;

quit;

## Re: How to use a Do loop when the code is a mixed between Proc/IML and other SAS/STAT

That may work. Thanks!

From The DO Loop