BookmarkSubscribeRSS Feed
RazzleBayker
Calcite | Level 5

I have several thousand observations.  I want a code for a regression that would work like a moving average; i need a regression for obs 1 through 200, then a regression for obs 2 through 201, then 3 through 202, etc etc.  Is this possible to do without making a seperate sample for each regression?  Any idea what the coding would look like?

Thanks in advance.

18 REPLIES 18
art297
Opal | Level 21

Not sure if there is a more direct way, but you could always wrap the reg call in a macro.  E.g., given sashelp.class which has 19 records, the following would run regressions on records 1 thru 5, then 2 thru 6, etc, thru 15 thru 19:

%macro repeatreg (number,lastset);

  %do i=1 %to &lastset;

    %let j=%eval(&i.+&number.);

    proc reg data=sashelp.class (firstobs=&i obs=&j.);

      model age=height weight;

    run;

    quit;

  %end;

%mend repeatreg;

%repeatreg(5,15)

Reeza
Super User

use by groups after classifying your observations into groups, you can use proc format to help.

proc sort data=sashelp.class; by sex;

proc reg data=sashelp.class;

by sex;

model age=height weight;

run;

quit;

PGStats
Opal | Level 21

Or use a view and POINT=

data rCars(keep=grp engineSize horsepower) / view=rCars;
do grp = 0 to nbCars-200;
     do j = 1 + grp to 200 + grp;
          set sashelp.cars nobs=nbCars point=j;
          output;
          end;
     end;
stop;
run;

proc reg data=rCars outest=eCars;
by grp;
model horsePower = enginesize;
run;

That way you don't create any new version of your dataset and you can collect all the results in a single dataset.

PG

PG
Reeza
Super User

You can tell the Canadians today....

art297
Opal | Level 21

I couldn't talk my wife into doing two thanksgivings this year.  Anyone have a spare turkey leg?

P.S.: I like your solution

Ksharp
Super User

ArthurT,

Actually I like your solution more than PG's . PG's code will generated lots of obs when it is a big data.

Ksharp

PGStats
Opal | Level 21

A view doesn't generate any data. At least, it doesn't have to. - PG

PG
Ksharp
Super User

PG,

Yeah, But when you implement PROC REG ,it will produce lots of obs in a dataset.

PGStats
Opal | Level 21

There won"t be much data produced unless you include an OUTPUT statement (to get predicted values or residuals). The OUTEST=eCars option produces a single observation per regression. You can also request the NOPRINT option to suppress printed output. - PG

PG
Ksharp
Super User

Hi. PG,

I do not think so.

when you implement REG ,  data=rCars  will generate a  concrete dataset even you cann't see it .

View only contains the sas code you wrote before, when sas use reg model ,it will also need a real dataset to feed REG. View can't do it , I guess so. Did you make a experiment for it ?

Ksharp

PGStats
Opal | Level 21

I tested both solutions that I proposed. I can't tell if a hidden dataset is created by executing the view or if some other kind of interprocess communication is used. It might depend on its size. - PG

PG
Ksharp
Super User

Hi, PG

I believe a concrete dataset in somewhere, VIEW only contains the sas code ,you can't feed it into REG .

when you execute PROC REG ,sas will generate a dataset to feed it . You can't walk around it by using View.

Regards.

Ksharp

PGStats
Opal | Level 21

Please check section Performance Considerations in

http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#n07vq6hmss6f67n1vhnn...

It states that when only sequential access is required, no actual dataset is created when reading a data step view.

PG

PG
Linlin
Lapis Lazuli | Level 10

Hi Art,

Sorry, no spare turkey leg from me. You can have something else, enjoy it! - Linlin

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 18 replies
  • 3095 views
  • 1 like
  • 7 in conversation