Robert
Robert

Reputation: 2231

How to iteratively run a SAS procedure with different subsets of data?

I would like to repeatedly run PROC REG with different subsets of an existing SAS dataset. Here's a simple example dataset:

DATA data_main;
  input trt depth year response;
  cards;
1 1 2014 1.1
1 2 2014 1.2
2 1 2014 1.3
2 2 2014 1.4
1 1 2013 2.2
1 2 2013 2.4
2 1 2013 2.6
2 2 2013 2.8
;
run;

For each combination of trt and depth I want to run this procedure, where current_data is the current combination of trt and depth:

PROC REG data = current_data;
    model response = year;
run;

And I want to capture the regression coefficients and p-values for all iterations in one dataset or text file.

The number of levels of input and trt is much greater in my actual dataset, so I'm trying to avoid manually coding each combination. Can someone explain to me how to do this?

Upvotes: 0

Views: 168

Answers (1)

Parfait
Parfait

Reputation: 107567

Consider running a macro iterating through the combinations of trt and depth. Below nested loop iteratively re-creates the current_data dataset and uses it in regression procedure outputting the corresponding result table. Adjust value ranges in loop limits as needed for all combinations:

%macro loopregression;
%do j = 1 %to 2;                 * TRT VALUES;
    %do i = 2013 %to 2014;       * DEPTH VALUES; 
        DATA current_data; 
            SET data_main;  
            if trt = &j;    
            if depth = &i;
        run;

        PROC REG data = current_data noprint outest=results&i&j;;
            model response = year;
        run;
    %end;
%end;
%mend loopregression;

%loopregression;

Upvotes: 1

Related Questions