Extracting A Business Cycle

/*--------------------------------------------------------------

                    SAS Sample Library

        Name: ucmex09.sas
 Description: Example program from SAS/ETS User's Guide,
              The UCM Procedure
       Title: Extracting A Business Cycle
     Product: SAS/ETS Software
        Keys: equally spaced univariate time series data
        PROC: UCM
       Notes:

--------------------------------------------------------------*/

data gdp;
   input Real_GDP CONS_EXP PRIVATE_INV @@;
   date = intnx('qtr', '01jan1947'd, _n_-1);
   format date yyq.;
   lpinv  = log(PRIVATE_INV);
   lgdp = log(Real_GDP);
   label lgdp = "log real GDP";
   datalines;
1570.5 1017.2 170.2
1568.7 1034.0 156.7
1568.0 1037.5 151.6
1590.9 1037.7 182.6
1616.1 1042.6 202.6
1644.6 1054.3 215.8
1654.1 1056.1 218.3
1658.0 1064.8 207.9
1633.2 1066.1 174.8
1628.4 1082.6 150.9
1646.7 1085.0 164.3
1629.9 1100.2 154.9
1696.8 1118.9 194.2
1747.3 1136.8 215.5
1815.8 1195.3 234.8
1848.9 1160.1 266.2
1871.3 1187.4 237.9
1903.1 1154.5 244.0
1941.1 1167.9 225.3
1944.4 1174.9 206.1
1964.7 1178.1 210.9
1966.0 1200.7 193.3
1978.8 1206.0 203.5
2043.8 1248.3 218.1
2082.3 1263.4 222.5
2098.1 1271.2 223.3
2085.4 1268.2 218.0
2052.5 1259.7 201.0
2042.4 1264.3 199.1
2044.3 1280.1 198.5
2066.9 1297.1 208.7
2107.8 1324.0 218.2
2168.5 1353.5 241.6
2204.0 1379.1 256.8
2233.4 1396.1 260.4
2245.3 1413.3 266.0
2234.8 1415.5 257.1
2252.5 1420.2 254.1
2249.8 1423.4 251.2
2286.5 1442.8 248.4
2300.3 1452.7 244.3
2294.6 1455.1 244.1
2317.0 1467.0 249.9
2292.5 1467.8 228.7
2230.2 1447.3 211.9
2243.4 1458.9 206.7
2295.2 1482.2 223.8
2348.0 1500.9 244.5
2392.9 1525.9 258.0
2455.8 1551.7 279.8
2453.9 1569.2 260.1
2462.6 1571.4 268.8
2517.4 1585.6 298.5
2504.8 1605.1 268.0
2508.7 1598.5 266.4
2476.2 1600.3 233.6
2491.2 1600.2 239.4
2538.0 1624.2 257.3
2579.1 1632.1 279.0
2631.8 1664.9 283.7
2679.1 1682.7 300.6
2708.4 1703.1 297.7
2733.3 1717.0 302.9
2740.0 1741.5 292.6
2775.9 1753.1 308.9
2810.6 1770.0 313.4
2863.5 1794.0 323.7
2885.8 1809.3 327.8
2950.5 1845.2 341.2
2984.8 1877.9 339.7
3025.5 1912.6 347.7
3033.6 1918.0 350.3
3108.2 1960.3 385.8
3150.2 1982.0 385.7
3214.1 2016.0 399.5
3291.8 2072.7 401.4
3372.3 2103.2 435.2
3384.0 2109.0 427.3
3406.3 2133.1 423.1
3433.7 2142.0 425.2
3464.1 2154.6 413.4
3464.3 2183.4 395.8
3491.8 2194.5 407.2
3518.2 2207.8 416.0
3590.7 2260.3 425.2
3651.6 2295.1 442.3
3676.5 2338.2 427.9
3692.0 2348.6 432.3
3750.2 2375.0 460.8
3760.9 2390.0 457.1
3784.2 2401.0 467.8
3766.3 2419.8 442.7
3760.0 2434.4 428.7
3767.1 2445.7 430.2
3800.5 2467.1 437.5
3759.8 2460.1 411.9
3864.1 2507.4 465.6
3885.9 2530.5 479.9
3916.7 2550.7 486.3
3927.9 2593.2 471.3
3997.7 2627.6 504.4
4092.1 2677.3 535.4
4131.1 2718.4 542.9
4198.7 2781.7 545.5
4305.3 2832.0 580.4
4355.1 2830.5 607.4
4331.9 2840.6 583.6
4373.3 2832.2 606.2
4335.4 2807.8 566.9
4347.9 2819.0 564.7
4305.8 2831.6 533.0
4288.9 2790.8 537.9
4237.6 2814.6 443.8
4268.6 2860.5 427.7
4340.9 2901.2 463.9
4397.8 2931.4 477.2
4496.8 2989.7 526.4
4530.3 3016.3 549.3
4552.0 3047.9 550.0
4584.6 3088.0 553.1
4640.0 3124.6 580.9
4731.1 3141.5 625.5
4815.8 3171.4 659.8
4815.3 3219.1 641.9
4830.8 3237.3 654.0
5021.2 3306.4 699.3
5070.7 3320.8 720.6
5137.4 3347.8 736.6
5147.4 3365.3 737.1
5152.3 3364.0 735.1
5189.4 3397.3 720.6
5204.7 3407.1 707.2
5221.3 3401.7 701.6
5115.9 3325.8 638.7
5107.4 3362.0 590.3
5202.1 3406.8 650.6
5307.5 3421.3 716.0
5266.1 3422.1 682.2
5329.8 3435.7 724.7
5263.4 3409.7 696.4
5177.1 3432.2 623.7
5204.9 3444.3 622.9
5185.2 3470.8 615.8
5189.8 3533.9 561.5
5253.8 3568.5 581.3
5372.3 3639.5 637.7
5478.4 3704.1 680.1
5590.5 3762.5 750.7
5699.8 3794.9 830.1
5797.9 3849.3 858.0
5854.3 3879.1 878.3
5902.4 3930.2 864.3
5956.9 3996.2 835.2
6007.8 4032.6 849.8
6101.7 4109.1 840.5
6148.6 4118.4 873.5
6207.4 4152.7 871.6
6232.0 4196.7 852.2
6291.7 4269.5 825.4
6323.4 4296.7 826.6
6365.0 4298.6 852.0
6435.0 4357.3 853.2
6493.4 4406.3 854.1
6606.8 4417.1 920.6
6639.1 4490.6 868.8
6723.5 4522.7 889.9
6759.4 4560.5 895.6
6848.6 4614.0 907.5
6918.1 4631.2 942.3
6963.5 4653.0 931.3
7013.1 4697.3 920.4
7030.9 4718.8 910.8
7112.1 4757.1 920.0
7130.3 4773.0 920.1
7130.8 4792.6 898.4
7076.9 4758.3 841.8
7040.8 4738.1 807.3
7086.5 4779.4 803.5
7120.7 4800.1 823.5
7154.1 4795.9 854.7
7228.2 4875.0 835.8
7297.9 4903.0 890.7
7369.5 4951.8 900.2
7450.7 5009.4 929.1
7459.7 5027.3 950.3
7497.5 5071.9 957.8
7536.0 5127.3 957.8
7637.4 5172.9 1007.3
7715.1 5230.3 1050.6
7815.7 5268.0 1112.0
7859.5 5305.7 1092.4
7951.6 5358.7 1143.2
7973.7 5367.2 1154.6
7988.0 5411.7 1123.8
8053.1 5458.8 1113.1
8112.0 5496.1 1144.4
8169.2 5544.6 1160.2
8303.1 5604.9 1220.0
8372.7 5640.7 1280.8
8470.6 5687.6 1276.1
8536.1 5749.1 1302.9
8665.8 5775.8 1389.6
8773.7 5870.7 1417.5
8838.4 5931.4 1440.7
8936.2 5996.8 1515.8
8995.3 6092.1 1491.7
9098.9 6165.7 1525.8
9237.1 6248.8 1563.0
9315.5 6311.3 1606.6
9392.6 6409.7 1607.8
9502.2 6476.7 1647.4
9671.1 6556.8 1708.4
9695.6 6661.3 1678.0
9847.9 6703.3 1788.6
9836.6 6768.0 1742.6
9887.7 6825.0 1732.7
9875.6 6853.1 1670.3
9905.9 6870.3 1637.4
9871.1 6900.5 1592.6
9910.0 7017.6 1493.4
9977.3 7042.2 1541.7
10031.6 7083.5 1549.0
10090.7 7123.2 1570.9
10095.8 7148.2 1567.0
10126.0 7184.9 1561.8
10212.7 7249.3 1574.4
10398.7 7352.9 1639.7
10467.0 7394.3 1676.5
10543.6 7475.1 1685.3
10634.2 7520.5 1766.3
10728.7 7585.5 1800.5
10796.4 7664.3 1828.8
10878.4 7709.4 1852.6
10954.1 7775.2 1834.3
11074.3 7852.8 1865.3
11107.2 7876.9 1924.9
11238.7 7961.9 1945.4
11306.7 8009.3 1948.5
11336.7 8063.8 1928.2
11395.5 8141.2 1856.2
11412.6 8215.7 1816.9
11507.9 8241.4 1831.0
  ;

proc ucm data=gdp;
    where year(date) >= 1970;
    ods output CycleDescription = cycledesc1;
    id date interval=quarter;
    model lgdp;
    irregular;
    level variance=0 noest plot=smooth;
    slope;
    cycle plot=smooth;
    estimate plot=panel;
    forecast plot=decomp outfor=for1;
run;

proc ucm data=gdp;
    where year(date) >= 1970;
    ods output CycleDescription = cycledesc2;
    id date interval=quarter;
    model lgdp;
    irregular;
    level variance=0 noest plot=smooth;
    slope;
    cycle order=2 plot=smooth;
    estimate plot=panel;
    forecast plot=decomp outfor=for2(
       rename=(
          s_level=s_level2
          vs_level=vs_level2
          s_cycle=s_cycle2
          vs_cycle=vs_cycle2));
run;

proc ucm data=gdp;
    where year(date) >= 1970;
    ods output CycleDescription = cycledesc4;
    id date interval=quarter;
    model lgdp;
    irregular;
    level variance=0 noest plot=smooth;
    slope;
    cycle order=4 plot=smooth;
    estimate plot=panel;
    forecast plot=decomp outfor=for4(
       rename=(
          s_level=s_level4
          vs_level=vs_level4
          s_cycle=s_cycle4
          vs_cycle=vs_cycle4));
run;

data combined;
  merge for1(keep=date s_cycle vs_cycle s_level vs_level)
        for2(keep=date s_cycle2 vs_cycle2 s_level2 vs_level2)
        for4(keep=date s_cycle4 vs_cycle4 s_level4 vs_level4);
        label s_cycle2 = "Smoothed Cycle (order=2)";
        label s_level2 = "Trend Extracted Based on Cycle of Order=2";
        label s_cycle4 = "Smoothed Cycle (order=4)";
        label s_level4 = "Trend Extracted Based on Cycle of Order=4";
run;

data c1;
   set cycledesc1(drop=_group_ Name Type);
   order = 1;
run;
data c2;
   set cycledesc2(drop=_group_ Name Type);
   order = 2;
run;
data c4;
   set cycledesc4(drop=_group_ Name Type);
   order = 4;
run;
proc append base=c1 data=c2;
proc append base=c1 data=c4;
run;

 proc print data=c1 noobs;
    var Order Period Frequency Rho ErrorVar;
 run;

proc sgplot data=combined;
   series x=date y=s_cycle;
   series x=date y=s_cycle2;
   series x=date y=s_cycle4;
run;