Here is a general solution to the problem you pose. It is based on my suggested data table format for your previous question. The code is appended. Program steps Create source table. Using data supplied in your question. Numbers for 2011 have been assigned to Q4 without affecting the outcome of this example. Year and Quarter values are stored as strings for simplicity and efficiency. Find last complete year. This is the max value for year where data for Q4 exists. The previous and next years are also calculated. (If no “next” year data exists, this will not disturb the process.) Summarise data. We want summaries for the years in question, by region, CU, year and quarter. Some of the data is redundant but will be screened out in the next step. The _type_ variable indicates the level of summarisation. Add category and group labels. Identify regional subtotals (ALL CU) and overall TOTALS per quarter and year. Dummy YOY records are created as placeholders in the table. The _name_ variable will be used for column headings by proc transpose. Sort into order for transposition. The sort ensures subtotals and totals come last at regional and overall level. Transpose data into table form. This spreads the selected quarterly and annual data across columns, instead of down rows. Calculate YOY and clean up. Calculate the YOY as required, add the subtotal and total labels and get rid of unwanted columns for cat, grp, and _name_. Richard in Oz /* Calculate_totals_YOY.sas */ /* Required calculation of region totals and totals by quarter and year, with year on year for the last complete year */ /* Richard C Carson Melbourne, Australia richard@arcol-analytics.com +61 412 836 040 */ /* Step 1: Create source table */ data have ; infile datalines dsd dlm='09'x ; informat region_nm $8. cu_nm $3. year $4. quarter $1. number best16. ; input region_nm $ cu_nm $ year $ quarter $ number ; datalines ; REGIONX 2011 4 14 REGIONX CUX 2011 4 3 REGIONX CUY 2011 4 4 REGIONX 2012 1 3 REGIONX CUX 2012 1 1 REGIONX CUY 2012 1 2 REGIONX 2012 2 4 REGIONX CUX 2012 2 2 REGIONX CUY 2012 2 2 REGIONX 2012 3 5 REGIONX CUX 2012 3 2 REGIONX CUY 2012 3 3 REGIONX 2012 4 6 REGIONX CUX 2012 4 0 REGIONX CUY 2012 4 0 REGIONX 2013 1 3 REGIONX CUX 2013 1 1 REGIONX CUY 2013 1 1 REGIONX 2013 2 4 REGIONX CUX 2013 2 2 REGIONX CUY 2013 2 0 ; /* Step 2: Find last complete year */ Proc SQL noprint ; Select put(max(input(year,4.)),4.) into :year1 From have Where quarter = '4' ; Quit ; %Put NOTE: Last complete year is &year1.. ; %Let year0 = %eval(&year1 - 1) ; %Let year2 = %eval(&year1 + 1) ; /* Step 3: Summarise data */ Proc Summary missing data = have ; where year in ("&year0", "&year1", "&year2") ; By year ; Class region_nm cu_nm quarter ; var number ; Output out = totals sum = ; Run ; /* Step 4: Add category and group labels */ Data totals2 ; Length _Name_ $ 8 ; Set totals ; By year ; Where (year = "&year0" and _type_ IN (0, 4, 6)) or (year = "&year1") or (year = "&year2" and _type_ IN (1, 3, 5, 7)) ; If quarter > ' ' then _name_ = cat('Y', year, 'Q', quarter) ; else _name_ = cat('Y', year) ; Select (_type_) ; When (0,1) cat = 'TOTAL' ; When (2,3) delete ; When (4,5) grp = 'ALL' ; Otherwise ; End ; If _name_ = "Y&year1" Then Do ; Output ; _name_ = CATT(_name_, 'YOY') ; Call missing (number) ; output ; End ; Else output ; Drop _freq_ ; Run ; /* Step 5: Sort into order for transposition */ Proc Sort Data = totals2 ; By cat region_nm grp cu_nm year descending _type_ quarter ; Run ; /* Step 6: Transpose data into table form */ Proc Transpose data = totals2 out = required ; By cat region_nm grp cu_nm ; Var number ; Run ; /* Step 7: Calculate YOY and clean up */ Data want ; set required ; If cat > ' ' Then region_nm = cat ; If grp > ' ' Then cu_nm = grp ; Y&year1.YOY = SUM(Y&year1, -Y&year0) ; Drop cat grp _name_ ; Run ;
... View more