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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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;

*add group id;data test;

  set test;

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

run;

proc corr data=test;

  var height weight;

  by group;

run;

View solution in original post

7 REPLIES 7
art297
Opal | Level 21

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;

*add group id;data test;

  set test;

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

run;

proc corr data=test;

  var height weight;

  by group;

run;

Doc_Duke
Rhodochrosite | Level 12

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

warnost
Calcite | Level 5

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

warnost
Calcite | Level 5

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

art297
Opal | Level 21

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.

*add group id;data test;

  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;

warnost
Calcite | Level 5

This worked great, thank you!

Cynthia_sas
SAS Super FREQ

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 5523 views
  • 3 likes
  • 4 in conversation