1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
| %MACRO BNelson(DATA=,OUT=,VAR=,DATE=,MAXPQ=,PAR=,QMA=,METHOD=,MAXITER=,
MCOEF=,MINMOD=,PRINT=,GRAPH=,CRAW=,CTREND=,CCYCLE=,DEBUG= );
/************************************************************************/
/* BNELSON: Cycle extraction with Beveridge-Nelson method. */
/************************************************************************/
/* This macro implements the Beveridge-Nelson decomposition model of a */
/* time series into Trend and Cycle. */
/* The basic output is a SAS dataset with the series and its components.*/
/* On request, you obtain some graphs. */
/*----------------------------------------------------------------------*/
/* Author: Dominique LADIRAY (July 2001) */
/************************************************************************/
/* MACRO PARAMETERS */
/************************************************************************/
/* */
/* DATA = SAS dataset containing the input time series. By default */
/* the _LAST_ dataset is used. */
/* OUT = Output SAS dataset with the series and its components. By */
/* default OUT=_result_. The components are named BN_Trendn */
/* for the trend and BN_Cyclen for the cycle where n refers */
/* to the ajusted ARIMA (p,1,q) model. In case several models */
/* are possible, a decomposition is done for each model (see */
/* MAXPQ). */
/* VAR = The time series to be decomposed. It must be numeric. */
/* DATE = This variable is used for the graphs and for the Ljung-Box */
/* test for residuals. Therefore, it must be precised and */
/* must be a SAS date variable (this point cannot be checked).*/
/* MAXPQ = You can ask for an automatic ajustment of the (p,1,q) */
/* ARIMA model. The orders p and q will be determined with */
/* the ESACF and SCAN method available in the ESTIMATE */
/* statement of PROC ARIMA. MAXPQ precises the maximum value */
/* of the orders p and q. If the parameters PAR or QMA and */
/* MAXPQ are not specified, then an automatic modelling is */
/* performed with MAXPQ=10. */
/* PAR = In case you want to specify the (p,1,q) ARIMA model to be */
/* ajusted, the parameter PAR= precises the order of the AR */
/* part. PAR must be positive. */
/* QMA = In case you want to specify the (p,1,q) ARIMA model to be */
/* ajusted, the parameter QMA= precises the order of the MA */
/* part. */
/* PAR and QMA must be precised together otherwise an */
/* automatic modelling is performed (see MAXPQ) */
/* METHOD = The method to be used in the ARIMA estimation. Possible */
/* values are CLS (the default), ULS and ML. */
/* MAXITER = Maximum number of iteration in the estimation. By default */
/* MAXITER=&maxiter. */
/* MCOEF = The adjusted ARIMA model must be converted in a MA model. */
/* MCOEF is the number of coefficients you require for the */
/* MA model. By default MCOEF=100. */
/* MINMOD = The adjusted ARIMA model must be invertible. The roots of */
/* the AR polynomial should be outside the unit circle. */
/* MINMOD is the minimum value required for the modulus of */
/* the roots. By default, MINMOD=0.98. */
/* PRINT = If YES, you get some printed output from the automatic */
/* ARIMA modeling. By default PRINT=yes. */
/* GRAPH = If YES, you get some graphs of the components. By default, */
/* GRAPH=yes. */
/* CRAW = Color of the raw series. Default BLACK. */
/* CTREND = Color of the Trend. Default RED. */
/* CCYCLE = Color of the cycle. Default BLUE. */
/* For these last three options, use a valid SAS/Graph color. */
/* DEBUG = Debugging option */
/* */
/************************************************************************/
/* REFERENCES : */
/* Beveridge S., Nelson C.R. (1981) */
/* A New Approach to Decomposition of Economic Time Series into */
/* Permanent and Transitory Components with Particular Attention to */
/* Measurement of the 'Business Cycle'. */
/* Journal of Monetary Economics, 7, pages 151-174. */
/************************************************************************/
%IF (&debug NE) %THEN %DO;OPTIONS MPRINT NOTES;%END;
/************************************************************************/
/* SASNOM Macro, to check if a name is a valid SAS name */
/************************************************************************/
%MACRO sasnom(_IN_=,TYPE=);
%GLOBAL _ret_;
%LET _ret_=0;
%IF &SYSVER < 7 %THEN %LET max=8;%ELSE %LET max=32;
%LET TABLE=%UPCASE(%TRIM(&_in_));
%IF (%UPCASE(&type) EQ DATASET) %THEN %DO;
%LET INDEX=%INDEX(&TABLE,.);
%IF (&INDEX GT 1) %THEN %DO;
%LET lib=%SUBSTR(&TABLE,1,&index-1);
%LET TABLE=%SUBSTR(&TABLE,&INDEX+1);
%LET c1=%LENGTH(&lib);
%LET c2=%VERIFY(%SUBSTR(&lib,1,1),'_ABCDEFGHIJKLMNOPQRSTUVWXYZ');
%LET c3=%VERIFY(&lib,'_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
%IF ((&c1 > &max) OR (&c2 > 0) OR (&c3 > 0)) %THEN %LET _ret_=1;
%END;
%END;
%LET c1=%LENGTH(&TABLE);
%LET c2=%VERIFY(%SUBSTR(&TABLE,1,1),'_ABCDEFGHIJKLMNOPQRSTUVWXYZ');
%LET c3=%VERIFY(&TABLE,'_0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ');
%IF ((&c1 > &max) OR (&c2 > 0) OR (&c3 > 0)) %THEN %LET _ret_=1;
%MEND;
/************************************************************************/
/* Reading and checking of parameters */
/************************************************************************/
/* Checking parameter data */
/************************************************************************/
%IF (%LENGTH(&DATA ) EQ 0) %THEN %LET DATA = _last_ ;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&DATA ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the DATA (&DATA ) parameter;
%GOTO finmacro;
%END;
DATA _NULL_;
SET &DATA (OBS=1);
RUN;
%IF (&syserr NE 0) %THEN %DO;
%PUT ERROR: There IS a problem IN the DATA (&DATA ) parameter;
%PUT ERROR: Non valid SAS dataset? Non valid SAS name? ;
%GOTO finmacro;
%END;
%END;
/* Checking parameter out */
/************************************************************************/
%IF (%LENGTH(&out ) EQ 0) %THEN %LET out = _result_ ;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&out ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the out (&out ) parameter;
%GOTO finmacro;
%END;
%sasnom(_IN_=&out ,TYPE=DATASET);
%IF (&_ret_ NE 0) %THEN %DO;
%PUT ERROR: The out (&out ) parameter must be a valid SAS dataset name;
%GOTO finmacro;
%END;
%END;
/* Checking parameter var */
/************************************************************************/
%IF (%LENGTH(&var ) EQ 0) %THEN %DO;
%PUT ERROR: The var parameter must be precised;
%GOTO finmacro;
%END;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&var ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the var (&var ) parameter;
%GOTO finmacro;
%END;
DATA _NULL_;
SET &DATA (KEEP=&var OBS=1);
ARRAY _xx_(*) &var ;
RUN;
%IF (&syserr NE 0) %THEN %DO;
%PUT ERROR: There IS a problem IN the var (&var ) parameter;
%PUT ERROR: Non valid SAS dataset? Non valid SAS name? Wrong variable type? Non valid SAS list?;
%GOTO finmacro;
%END;
%END;
/* Checking parameter date */
/************************************************************************/
%IF (%LENGTH(&date ) EQ 0) %THEN %DO;
%PUT ERROR: The date parameter must be precised;
%GOTO finmacro;
%END;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&date ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the date (&date ) parameter;
%GOTO finmacro;
%END;
DATA _NULL_;
SET &DATA (KEEP=&date OBS=1);
ARRAY _xx_(*) &date ;
RUN;
%IF (&syserr NE 0) %THEN %DO;
%PUT ERROR: There IS a problem IN the date (&date ) parameter;
%PUT ERROR: Non valid SAS dataset? Non valid SAS name? Wrong variable type? Non valid SAS list?;
%GOTO finmacro;
%END;
%END;
/* Checking parameter maxpq */
/************************************************************************/
%IF (%LENGTH(&maxpq ) EQ 0) %THEN %LET maxpq = 10 ;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&maxpq ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the maxpq (&maxpq ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&maxpq ) EQ CHAR) %THEN %DO;
%PUT ERROR: The maxpq (&maxpq ) parameter must be numeric;
%GOTO finmacro;
%END;
%ELSE %IF (&maxpq LE 0 ) %THEN %DO;
%PUT ERROR: You must have &maxpq > 0 ;
%GOTO finmacro;
%END;
%END;
/* Checking parameter mcoef */
/************************************************************************/
%IF (%LENGTH(&mcoef ) EQ 0) %THEN %LET mcoef = 100 ;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&mcoef ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the mcoef (&mcoef ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&mcoef ) EQ CHAR) %THEN %DO;
%PUT ERROR: The mcoef (&mcoef ) parameter must be numeric;
%GOTO finmacro;
%END;
%ELSE %IF (&mcoef LE 0 ) %THEN %DO;
%PUT ERROR: You must have mcoef > 0 ;
%GOTO finmacro;
%END;
%END;
/* Checking parameter par */
/************************************************************************/
%IF (%LENGTH(&par ) NE 0) %THEN %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&par ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the par (&par ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&par ) EQ CHAR) %THEN %DO;
%PUT ERROR: The par (&par ) parameter must be numeric;
%GOTO finmacro;
%END;
%ELSE %IF (&par LT 0 ) %THEN %DO;
%PUT ERROR: You must have par >= 0 ;
%GOTO finmacro;
%END;
%END;
/* Checking parameter qma */
/************************************************************************/
%IF (%LENGTH(&qma) NE 0) %THEN %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&qma ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the qma (&qma ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&qma ) EQ CHAR) %THEN %DO;
%PUT ERROR: The qma (&qma ) parameter must be numeric;
%GOTO finmacro;
%END;
%ELSE %IF (&qma LT 0 ) %THEN %DO;
%PUT ERROR: You must have qma >= 0 ;
%GOTO finmacro;
%END;
%END;
/* Checking parameter method */
/************************************************************************/
%IF (%LENGTH(&method ) EQ 0) %THEN %LET method = CLS;
%ELSE %DO;
%LET method=%UPCASE(&method);
%IF (%LENGTH(%QSCAN(%BQUOTE(&method ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the method (&method ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&method ) EQ NUM) %THEN %DO;
%PUT ERROR: The method (&method ) parameter must be character;
%GOTO finmacro;
%END;
%ELSE %IF ((&method NE CLS) AND (&method NE ULS) AND (&method NE ML)) %THEN %DO;
%PUT ERROR: The method (&method ) parameter must be choosen among (CLS ULS ML);
%GOTO finmacro;
%END;
%END;
/* Checking parameter maxiter */
/************************************************************************/
%IF (%LENGTH(&maxiter ) EQ 0) %THEN %LET maxiter = 100 ;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&maxiter ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the maxiter (&maxiter ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&maxiter ) EQ CHAR) %THEN %DO;
%PUT ERROR: The maxiter (&maxiter ) parameter must be numeric;
%GOTO finmacro;
%END;
%ELSE %IF (&maxiter LE 0 ) %THEN %DO;
%PUT ERROR: You must have maxiter > 0 ;
%GOTO finmacro;
%END;
%END;
/* Checking parameter print */
/************************************************************************/
%IF (%LENGTH(&print ) EQ 0) %THEN %LET print = YES ;
%ELSE %DO;
%LET print=%UPCASE(&print);
%IF (%LENGTH(%QSCAN(%BQUOTE(&print ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the print (&print) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&print ) EQ NUM) %THEN %DO;
%PUT ERROR: The print (&print) parameter must be character;
%GOTO finmacro;
%END;
%ELSE %IF ((&print NE YES) AND (&print NE NO)) %THEN %DO;
%PUT ERROR: The print (&print) parameter must be choosen among (YES NO);
%GOTO finmacro;
%END;
%END;
/* Checking parameter graph */
/************************************************************************/
%IF (%LENGTH(&graph ) EQ 0) %THEN %LET graph = YES ;
%ELSE %DO;
%LET graph=%UPCASE(&graph);
%IF (%LENGTH(%QSCAN(%BQUOTE(&graph ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the graph (&graph) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&graph ) EQ NUM) %THEN %DO;
%PUT ERROR: The graph (&graph) parameter must be character;
%GOTO finmacro;
%END;
%ELSE %IF ((&graph NE YES) AND (&graph NE NO)) %THEN %DO;
%PUT ERROR: The graph (&graph) parameter must be choosen among (YES NO);
%GOTO finmacro;
%END;
%END;
/* Checking parameter craw */
/************************************************************************/
%IF (%LENGTH(&craw ) EQ 0) %THEN %LET craw = black ;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&craw ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the craw (&craw ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&craw ) EQ NUM) %THEN %DO;
%PUT ERROR: The craw (&craw ) parameter must be character;
%GOTO finmacro;
%END;
%END;
/* Checking parameter ctrend */
/************************************************************************/
%IF (%LENGTH(&ctrend ) EQ 0) %THEN %LET ctrend = red ;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&ctrend ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the ctrend (&ctrend ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&ctrend ) EQ NUM) %THEN %DO;
%PUT ERROR: The ctrend (&ctrend ) parameter must be character;
%GOTO finmacro;
%END;
%END;
/* Checking parameter ccycle */
/************************************************************************/
%IF (%LENGTH(&ccycle ) EQ 0) %THEN %LET ccycle = blue ;
%ELSE %DO;
%IF (%LENGTH(%QSCAN(%BQUOTE(&ccycle ),2,%STR( ))) NE 0) %THEN %DO;
%PUT ERROR: You must precise just one value FOR the ccycle (&ccycle ) parameter;
%GOTO finmacro;
%END;
%IF (%DATATYP(&ccycle ) EQ NUM) %THEN %DO;
%PUT ERROR: The ccycle (&ccycle ) parameter must be character;
%GOTO finmacro;
%END;
%END;
/************************************************************************/
/* Some other controls */
/************************************************************************/
%LET erreur=0;
DATA _NULL_;
IF (INT(&mcoef ) ^= &mcoef ) THEN CALL SYMPUT('erreur','1');
IF (INT(&maxpq ) ^= &maxpq ) THEN CALL SYMPUT('erreur','1');
IF (INT(&maxiter ) ^= &maxiter ) THEN CALL SYMPUT('erreur','1');
%IF (&par NE) %THEN %DO;
IF (INT(&par ) ^= &par ) THEN CALL SYMPUT('erreur','1');
%END;
%IF (&qma NE) %THEN %DO;
IF (INT(&qma ) ^= &qma ) THEN CALL SYMPUT('erreur','1');
%END;
RUN;
%IF (&erreur EQ 1) %THEN %DO;
%PUT ERROR: the parameters mcoef, par, qma AND maxpq must be integer.
%GOTO finmacro;
%END;
/************************************************************************/
/* Controls end, Macro begins */
/************************************************************************/
%IF (%UPCASE(&print) NE YES)
%THEN %LET print=noprint;
%ELSE %LET print=;
/* We check for missing values and compute the series periodicity. */
/************************************************************************/
PROC FORMAT;
VALUE njm
25-35=12 50-70=6 80-100=4 115-135=3 175-195=2;
RUN;
%LET _miss_=0;
DATA _trav_;
SET &DATA END=fin;
KEEP &date &var;
RETAIN dtmin dtmax;
IF (&var ^=.) THEN DO;
ndon+1;
dtmax=&date;
IF (ndon=1) THEN dtmin=&date;
END;
IF ((&var ^= .) AND (LAG(&var) =.) AND (ndon > 1)) THEN CALL SYMPUT('_miss_','1');
IF fin THEN DO;
period=PUT((dtmax-dtmin)/ndon,njm.);
CALL SYMPUT('period',period);
END;
IF (&var ^=.);
RUN;
%IF (&period EQ ) %THEN %DO;
%PUT ERROR: Problem WITH the automatic determination of the series periodicity. CHECK your SAS date variable;
%GOTO finmacro;
%END;
%IF (&_miss_ EQ 1) %THEN %DO;
%PUT ERROR: Missing VALUES IN series &var;
%GOTO finmacro;
%END;
/* Non automatic ARIMA modeling of the series. */
/************************************************************************/
%IF ((&par NE) AND (&qma NE)) %THEN %DO;
DATA _bic_;
Model=1;
ar=∥
ma=&qma;
RUN;
%LET _nmod=1;
%LET _maxar=∥
%LET _maxma=&qma;
%LET _ar1=∥
%LET _ma1=&qma;
PROC ARIMA DATA=_trav_;
ODS SELECT NONE;
ODS OUTPUT ChiSqAuto=ChiSq ParameterEstimates=ParEst;
IDENTIFY VAR=&var(1);
ESTIMATE P=&par Q=&qma METHOD=&method MAXITER=&maxiter;
FORECAST ID=&date OUT=_arima_(KEEP=&date &var residual RENAME=(residual=Resid1)) LEAD=0;
QUIT;
ODS SELECT ALL;
%END;
/* Automatic ARIMA modeling of the series. */
/************************************************************************/
%ELSE %DO;
PROC ARIMA DATA=_trav_;
ODS SELECT NONE;
ODS OUTPUT TentativeOrders=_bic_ ;
IDENTIFY VAR=&var(1) P=&maxpq Q=&maxpq ESACF SCAN;
RUN;
QUIT;
ODS SELECT ALL;
%LET _ar=0;
%LET _ma=0;
DATA _bic_;
SET _bic_(KEEP=scan_AR scan_MA RENAME=(scan_AR=ar scan_MA=ma))
_bic_(KEEP=esacf_AR esacf_MA RENAME=(esacf_AR=ar esacf_MA=ma));
KEEP ar ma sum;
IF ar > 0;
sum=ar+ma;
RUN;
PROC SORT DATA=_bic_ OUT=_bic_ NODUP;
BY sum ma ar;
RUN;
%LET _nmod=0;
DATA _bic_;
LENGTH Model 8;
RETAIN maxar maxma 0;
SET _bic_;
KEEP Model ar ma;
model+1;
maxar=MAX(ar,maxar);
maxma=MAX(ma,maxma);
CALL SYMPUT('_ar'||LEFT(PUT(model,3.)),LEFT(PUT(ar,2.)));
CALL SYMPUT('_ma'||LEFT(PUT(model,3.)),LEFT(PUT(ma,2.)));
CALL SYMPUT('_nmod',LEFT(PUT(model,3.)));
CALL SYMPUT('_maxar',LEFT(PUT(maxar,3.)));
CALL SYMPUT('_maxma',LEFT(PUT(maxma,3.)));
RUN;
%IF (&_nmod EQ 0) %THEN %DO;
%PUT ERROR: No automatic model has been proposed BY ESACF AND SCAN options;
%GOTO finmacro;
%END;
PROC ARIMA DATA=_trav_;
ODS SELECT NONE;
ODS OUTPUT ChiSqAuto=ChiSq ParameterEstimates=ParEst;
IDENTIFY VAR=&var(1);
%DO i=1 %TO &_nmod;
ESTIMATE P=&&_ar&i Q=&&_ma&i METHOD=&method MAXITER=&maxiter;
FORECAST ID=&date OUT=for&i(KEEP=&date &var residual) LEAD=0;
%END;
RUN;
QUIT;
ODS SELECT ALL;
DATA _arima_;
MERGE
%DO k=1 %TO &_nmod;
for&k(RENAME=(residual=Resid&k))
%END;
;
BY &date;
LABEL
%DO k=1 %TO &_nmod;
resid&k=' ' forecast&k=' '
%END;
;
RUN;
%END;
/* We check now the validity of the ARIMA models. */
/************************************************************************/
DATA ParEst;
SET ParEst;
RETAIN model 0;
IF Parameter=:'MU' THEN model=model+1;
Parameter=SUBSTR(Parameter,1,2)||LEFT(PUT(Lag,3.));
RUN;
PROC TRANSPOSE DATA=ParEst(KEEP=Estimate Parameter Model) OUT=ParEst;
BY model;
ID Parameter;
RUN;
DATA ChiSq;
LENGTH Lag $ 8;
SET ChiSq;
KEEP Lag ProbChiSq Model ToLags;
RETAIN Model -1 n;
IF (ToLags=6) THEN DO;
model=model+1;
n=0;
END;
LAG='PLjung';
IF model > 0;
IF df;
IF (((&period=12) AND (ToLags=12)) OR ((&period^=12) AND (ToLags=6))
OR ((n=0) AND (ToLags > &period)))
THEN DO;
OUTPUT;
n=1;
END;
RUN;
PROC TRANSPOSE DATA=ChiSq OUT=ChiSq;
BY model;
ID Lag;
COPY ToLags;
RUN;
DATA ChiSq;
SET ChiSq;
LjungBox= (PLjung > 0.05);
RUN;
%LET _selmod=0;
DATA _bic_;
RETAIN n 0;
LENGTH Model 8 Arima $ 10;
MERGE _bic_ ParEst ChiSq;
DROP _NAME_ _LABEL_ n;
BY Model;
LABEL ar=' ' ma=' ';
Arima=COMPRESS('('||PUT(ar,3.)||',1,'||PUT(ma,3.)||')');
IF (LjungBox) THEN DO;
IF (n=0) THEN CALL SYMPUT('_selmod',LEFT(PUT(Model,2.)));
n=n+1;
selmod='*';
END;
RUN;
%IF (&print EQ) %THEN %DO;
DATA _bic1_;
LENGTH Arima $ 10 Selmod $1 LjungBox PLjung ToLags MU AR1-AR&_maxar MA1-MA&_maxma 8;
SET _bic_(RENAME=(MU0=MU));
ARRAY num{*} _NUMERIC_;
DROP i model ar ma;
DO i= 1 TO DIM(num);
num(i)=ROUND(num(i),0.001);
END;
RUN;
PROC TRANSPOSE DATA=_bic1_ OUT=_bic1_(DROP=_LABEL_) PREFIX=Model;
VAR _all_;
RUN;
DATA _bic1_;
SET _bic1_;
DROP i;
ARRAY model{*} $ model1-model&_nmod;
DO i= 1 TO DIM(model);
model(i)=RIGHT(model(i));
END;
LABEL _name_=' ';
RENAME _name_=Description;
RUN;
PROC PRINT DATA=_bic1_;
ID Description;
TITLE "Possible ARIMA models";
RUN;
TITLE;
%END;
%IF (&_selmod EQ 0) %THEN %DO;
%PUT ERROR: No automatic model has been selected;
%GOTO finmacro;
%END;
/* We compute Trend and Cycle estimates with PROC IML. */
/************************************************************************/
PROC IML;
USE _bic_;
READ ALL VAR{%DO i=1 %TO &_maxar; ar&i %END;} INTO ar;
READ ALL VAR{%DO i=1 %TO &_maxma; ma&i %END;} INTO ma;
READ ALL VAR{selmod} INTO selmod;
CLOSE _bic_;
USE _arima_;
READ ALL VAR{&var} INTO y;
READ ALL VAR{&date} INTO date;
READ ALL VAR{%DO i=1 %TO &_nmod; Resid&i %END;} INTO r;
CLOSE _arima_;
model=LOC(selmod='*');
nbmod=NCOL(model);
noms=CONCAT(J(1,nbmod,'BN_Trend'),CHAR(model,4));
noms=noms || CONCAT(J(1,nbmod,'BN_Cycle'),CHAR(model,4));
noms=COMPRESS(noms);
ar=J(nbmod,1,1)||(-ar)[model,];
ma=J(nbmod,1,1)||(-ma)[model,];
r=r[,model];
nobs=NROW(r);
npsi=&mcoef;
psi=J(npsi+1,nbmod,.);
cusum=J(npsi,nbmod,.);
DO i=1 TO nbmod;
cc=LOC(ar[i,]^=.);
dd=LOC(ma[i,]^=.);
psi[,i]=T(RATIO(ar[i,cc],ma[i,dd],npsi+1));
psi2=psi[2:npsi+1,i];
cusum[,i]=CUSUM(psi2[npsi:1])[npsi:1];
END;
cycle=J(nobs,nbmod,.);
maxobs=MIN(npsi,nobs);
DO i= nobs TO 2 BY -1;
min=MIN(i,maxobs);
rsub=r[i:MAX(i-min+1,2),];
csub=cusum[1:NROW(rsub),];
cycle[i,]=(rsub # csub)[+,];
END;
result=date || y || (REPEAT(y,1,nbmod)-cycle) || cycle;
names={"&date" "&var"} || noms;
CREATE &out FROM result[COLNAME=names];
APPEND FROM result;
CLOSE &out;
QUIT;
/* Graphs on request. */
/************************************************************************/
%IF (%UPCASE(&graph) EQ YES) %THEN %DO;
%IF %SYSFUNC(CEXIST(&var)) %THEN %DO;
PROC CATALOG C=&var KILL;
QUIT;
%END;
DATA _NULL_;
SET _bic_(WHERE=(selmod='*'));
CALL SYMPUT('model'||LEFT(PUT(_N_,3.)),arima);
CALL SYMPUT('trend'||LEFT(PUT(_N_,3.)),'BN_Trend'||LEFT(PUT(model,3.)));
CALL SYMPUT('cycle'||LEFT(PUT(_N_,3.)),'BN_Cycle'||LEFT(PUT(model,3.)));
CALL SYMPUT('nmod',LEFT(PUT(_N_,3.)));
RUN;
GOPTIONS CBACK=white BORDER noDISPLAY DEVICE=win FTEXT=swissb noPROMPT;
SYMBOL1 I=JOIN COLOR=&craw WIDTH=1 VALUE=none ;
SYMBOL2 I=JOIN COLOR=&ctrend WIDTH=1 VALUE=none ;
SYMBOL3 I=JOIN COLOR=&ccycle WIDTH=1 VALUE=none ;
AXIS1 LABEL=NONE;
PROC GPLOT DATA=&out GOUT=&var UNIFORM;
%DO i=1 %TO &nmod;
PLOT &var*&date &&trend&i*&date /FRAME NAME="orig_s&i" VAXIS=axis1 OVERLAY;
FORMAT &date MONYY5.;
TITLE "Variable &var and Trend, model &&model&i";
RUN;
PLOT &&cycle&i*&date /FRAME NAME="cycle&i" VAXIS=axis1;
FORMAT &date MONYY5.;
TITLE "Cycle of variable &var, model &&model&i";
RUN;
%END;
QUIT;
GOPTIONS display;
PROC GREPLAY IGOUT=&var TC=&var NOFS GOUT=&var;
TDEF newtwo DES='two squares of equal size'
1/LLX=1 LLY=50 ULX=1 ULY=99 URX=99 URY=100 LRX=99 LRY=50 color=grey
2/LLX=1 LLY=1 ULX=1 ULY=50 URX=99 URY=50 LRX=99 LRY=1 color=grey
;
TEMPLATE newtwo;
%DO i=1 %TO &nmod;
TREPLAY 1:orig_s&i 2:cycle&i;
RUN;
%END;
QUIT;
PROC GPLOT DATA= &out GOUT=&var UNIFORM;
%DO i=1 %TO &nmod;
PLOT &var*&date &&trend&i*&date /FRAME NAME="both&i" VAXIS=axis1 OVERLAY;
PLOT2 &&cycle&i*&date /FRAME NAME="double&i" VAXIS=axis1;
FORMAT &date MONYY5.;
TITLE "Variable &var, Estimated Trend and Cycle, model &&model&i";
RUN;
%END;
QUIT;
TITLE;
SYMBOL;
%END;
%finmacro:
%MEND; |