Programming the statistical procedures from SAS

Do loop to increase years

Reply
Contributor Moh
Contributor
Posts: 20

Do loop to increase years

Hi, folks - 

I want to develop some statistical models using different training samples and then test their forecasting power on different holdout sample. For this purpose, I used window rolling approach in where both training and holdout sample move ahead untile specific year.

%let LT=2000;

%let UT=2005;

%let LH=2006;

%let UH=2008;

%macro_static_models(LT=&LT, UT=&UT, LH=&LH, UH=&UH);

so in the next round, I want to move one year ahead and have;

%let LT=2001;

%let UT=2006;

%let LH=2007;

%let UH=2009;

% macro_static_models (LT=&LT, UT=&UT, LH=&LH, UH=&UH) ;

 

the LT will stop at 2009. Do you have any suggestion how can I incorporate Do loop?

Thanks

Grand Advisor
Posts: 16,852

Re: Do loop to increase years

Use call execute.

 

data want;

lt=1999; ut=2004;

do i=1 to 10;
   lt=lt+1;
   ut=ut+1;

    str=cats('%macro_static_models(LT=', lt, ',UT=', UT, ');');

    call execute(str);

end;
run;



Respected Advisor
Posts: 4,606

Re: Do loop to increase years

Using call execute to call a macro is safer if you prevent macro expansion at the call time with %nrstr

 


data _null_;
length line $200;
do lt = 2000 to 2009;
    line = cats('%nrstr(%macro_static_models(LT=', lt, ',UT=', lt+5, ',LH=', lt+6, ',UH=', lt+8, '));');
    call execute(line);
    end;
run;
PG
Contributor Moh
Contributor
Posts: 20

Re: Do loop to increase years

many thanks for your reply. Actually, I forgot to add something else. I need to define new libraries for each round of tests. 

I use the last two digits of each year as a proxy for lib names, e.g. S&LTS&UHS&Y.C.

 

%let LT=2000;

%let UT=2005;

%let LH=2006;

%let UH=2008;

%let LTS=00;

%let UTS=05;

%let LHS=06;

%let UHS=08;

 

%macro_static_models(LT=&LT, UT=&UT, LH=&LH, UH=&UH);

 

Would you please kindly help me ? thanks

Grand Advisor
Posts: 16,852

Re: Do loop to increase years

I'm not sure how that changes anything. You can use the libname function inside a data null step to create the library based on the LT/UT variables.

 


Why are you creating macro variables outside your process, its better to keep it internal to your process.

 

data _null_;

lts=substr("&lt", 3, 2);
uts=substr("&ut", 3, 2);

rc=libname(cats(dir, lts, uts));

run;
Ask a Question
Discussion stats
  • 4 replies
  • 244 views
  • 0 likes
  • 3 in conversation