## Can you loop a procedure such as PROC CORR?

Solved
Occasional Contributor
Posts: 19

# Can you loop a procedure such as PROC CORR?

Hi,

I have a large data set and I am trying to run correlations every 60 observations, so far I have written this:

proc corr data=warnost.currency (firstobs=5614 obs=5673);

var /* my variables */;

run;

This runs a proc corr on the last 60 days. Below is what I am trying to accomplish, but I don't think you can do a loop with the proc corr inside it (syntax is horrible, I am just trying to convey my idea). Any ideas?

first = 5614

last = 5673

Do Until first < 0;

proc corr data=warnost.currency (firstobs=first obs=last);

var /* my variables */;

run;

first = first - 60;

last = last -60;

Loop;

Accepted Solutions
Solution
‎10-27-2011 10:14 AM
PROC Star
Posts: 8,164

## Can you loop a procedure such as PROC CORR?

Can't you just use the mod function to create your groups and then use group as a by variable within one proc corr?  e.g.,

*create some test data;

data test;

set sashelp.class;

do i=1 to 20;

output;

end;

run;

set test;

if mod(_n_,60) eq 1 then group+1;

run;

proc corr data=test;

var height weight;

by group;

run;

All Replies
Solution
‎10-27-2011 10:14 AM
PROC Star
Posts: 8,164

## Can you loop a procedure such as PROC CORR?

Can't you just use the mod function to create your groups and then use group as a by variable within one proc corr?  e.g.,

*create some test data;

data test;

set sashelp.class;

do i=1 to 20;

output;

end;

run;

set test;

if mod(_n_,60) eq 1 then group+1;

run;

proc corr data=test;

var height weight;

by group;

run;

Posts: 2,125

## Can you loop a procedure such as PROC CORR?

If disk space is a concern, you can use a data step view to create the mod'd group that Art suggests.  Either of those would be much more efficient than a looped PROC CORR (which passes the data each time it executes).

Doc Muhlbaier

Duke

Occasional Contributor
Posts: 19

## Re: Can you loop a procedure such as PROC CORR?

Disk space is not a concern but I would like to be efficient.

Occasional Contributor
Posts: 19

## Re: Can you loop a procedure such as PROC CORR?

Art,

This looks like it is what I need, but could you write a little more about what the code does? I have only taken programming 1 so I am not sure I fully understand what you have written. It looks like it is assigning groups during the data step which I then use as an option in the proc corr. Is that the idea?

Thank you

PROC Star
Posts: 8,164

## Re: Can you loop a procedure such as PROC CORR?

Yes, except for the terminology.  First, in a data step, you create the variable group.  mod(_n_,60) will only equal 1 every 60 records beginning with the first record.  Since I used the statement group+1, the value is automatically retained across records, thus the first 60 records get the value 1, the next 60 the value 2, etc.

set test;

if mod(_n_,60) eq 1 then group+1;

run;

Then, in the proc corr, including the statement "by group;" takes advantage of the extremely powerfull "by" group processing in SAS.   It simply tells SAS to re-run the requested analysis for each level of the by group.  Normally, you would first have to sort the data according to the "by" group but, in this case, the data by definition are already sorted.

proc corr data=test;

var height weight;

by group;

run;

Occasional Contributor
Posts: 19

## Re: Can you loop a procedure such as PROC CORR?

This worked great, thank you!

SAS Super FREQ
Posts: 9,370

## Re: Can you loop a procedure such as PROC CORR?

Hi:

SAS has a "meta" language called the SAS Macro Facility. It allows you to automate the creation of SAS code. In order to generate a SAS macro program, you first need to start with a working SAS program. Let's say you only have 180 observations and you want to run PROC CORR on 60 obs at a time, you would need to manually type all the PROC CORR steps, varying the firstobs and obs values for each step. Something like this:

ods listing;

proc corr data=sashelp.shoes(firstobs=1 obs=60);

var sales returns;

run;

proc corr data=sashelp.shoes(firstobs=61 obs=120);

var sales returns;

run;

proc corr data=sashelp.shoes(firstobs=121 obs=180);

var sales returns;

run;

So this is tedious, but do-able. At least, once you have working code, you can see that some kind of loop structure would be useful.

The SAS Macro facility allows you to generate SAS code using macro variables and macro programming logic inside a SAS macro program. In order to really use the Macro facility effectively, you have to understand the basics of creating and running a macro program. This paper has a very good introduction to SAS Macro processing.

http://www2.sas.com/proceedings/sugi28/056-28.pdf

There are a couple of ways to write the Macro program or you could use CALL EXECUTE from inside a DATA step program to construct the code to execute after the DATA step finishes. But, before you go down this road, you should read up on the Macro facility and code generation techniques. A Google search should net you a lot of links to SAS Global Forum paper on the topic.

cynthia

🔒 This topic is solved and locked.