BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Salah
Quartz | Level 8

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
Rick_SAS
SAS Super FREQ

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;

View solution in original post

6 REPLIES 6
Rick_SAS
SAS Super FREQ

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;
Salah
Quartz | Level 8

Thank you for your reply.

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

 

Rick_SAS
SAS Super FREQ

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;
PaigeMiller
Diamond | Level 26

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

use SUBMIT in IML.

 

proc iml;

.......

 

submit ;

 proc reg data=class;

 ...........

endsub;

 

quit;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 6 replies
  • 868 views
  • 0 likes
  • 4 in conversation