Adrian
Adrian

Reputation: 9803

Nested do loop in SAS

I want to generate 100 samples of 25 observations each of pseudonormal random variables using the rannor function. So here, 100= # of samples, and 25=sample size.

%macro bootstrap(bootsample, sample, samplesize);
  data &bootsample;
  %do i=1 %to &sample;
     do j=1 to &samplesize;
     obsnumber=ceil(&samplesize*rannor(642014));
     output;
     end;
     proc univariate data=&bootsample normal;
     ods output TestsForNormality=tests;
     var obsnumber;
     histogram obsnumber;
     run;
   %end;
   run;
   %mend;

%bootstrap(bootsample,100,25);

But I keep on receiving the error: Statement is not valid or it is used out of proper order. Where did I go wrong?

Upvotes: 0

Views: 1848

Answers (1)

user3645882
user3645882

Reputation: 739

Just move the first %do

%macro bootstrap(bootsample, sample, samplesize);
      %do i=1 %to &sample;
      data &bootsample;
         do j=1 to &samplesize;
         obsnumber=ceil(&samplesize*rannor(642014));
         output;
         end;
         proc univariate data=&bootsample normal;
         ods output TestsForNormality=tests;
         var obsnumber;
         histogram obsnumber;
         run;
       %end;
       run;
       %mend;

       %bootstrap(bootsample,100,25);

or

%MACRO BOOTSTRAP(SAMPLES=,SIZE=);
data NormalBootstrap;
    do sample=1 to &SAMPLES;
        do size=1 to &SIZE;
            value=rannor(642014);
            output;
        end;
    end;
run;
%MEND;
%BOOTSTRAP(SAMPLES=100,SIZE=25);
 proc univariate data=NormalBootstrap normal;
         ods output TestsForNormality=tests;
         var value;
         by sample;
         histogram value;
run;

(I do not have sas graph so I submitted the code above without the histogram statement)

Upvotes: 3

Related Questions