400 likes | 603 Views
Compute Blocks Revealed. Sandy McNeill. Agenda. Uses of Compute Blocks What do Compute Blocks look like? Rules Tricks/Traps Questions. What are the uses of Compute Blocks?. Compute Block for Computed Variable. Compute Block for Traffic Lighting. Compute Block for Line Statements.
E N D
Compute Blocks Revealed Sandy McNeill
Agenda • Uses of Compute Blocks • What do Compute Blocks look like? • Rules • Tricks/Traps • Questions
define balance / computed ; compute balance; balance=budget-actual; endcomp; compute before; totalaccts = 0; endcomp; As Simple As…. OR
As Cool As…. compute yrtodate; yrtodate=budget.sum-actual.sum; if yrtodate > 0 then call define( _col_, 'style', 'style={background=yellow}'); endcomp;
compute after dept; pctbud = round((budget.sum/totalbud)*100); updept = upcase(dept); line pctbud 2. '% of YTD budget allocated to ' updept $varying. len '.'; if yrtodate < 0 then do; pctover = abs(round((yrtodate/budget.sum)*100)); text2 = ‘Over budget by ' || trim(left(put(pctover,3.0))) || '%.'; end; else text2=' '; line text2 $80.; endcomp; As Complicated As…..
Evaluation / Execution 1 2 3
Values to the Left Column 3 is a computed column Can’t use COL4 value in COL3 BUT….. You can write to columns to the left
Analysis Variable “Dot” Syntax Weight.sum Height.median Weight.min Weight.max
Where can you use a Compute Block? • Before / After report • Compute before; • Compute after; • For any report variable • Compute weight; /* analysis variable */ • Compute age; /* group or order variable */ • Compute balance; /* computed variable */
Where or With What Variable (cont) • Before / After a grouping • Compute before age; • Compute after age; • Before / After Page or Table • Compute before _page_; • Compute after _page_;
Four Meanings of Analysis Vars • Compute before/after _page_ • Detail lines • Compute before/after Grouping • Compute before/after (report)
Compute Before / After _PAGE_ Compute Before _page_; Line ‘Budget.sum is ‘ budget.sum dollar12.2; Endcomp; Output looks like: Budget.sum is $40,000.00
Compute Before / After _PAGE_ Compute Before _page_; Line ‘Budget.sum is ‘ budget.sum dollar12.2; Endcomp; Output looks like: Budget.sum is $40,000.00 Where does REPORT get the value for Budget.sum ?
Detail Lines Detail 1 Detail 2 Detail 3 Summary Compute Computed; Computed = Analysis.sum; Endcomp;
Detail Lines Detail 1 Detail 2 Detail 3 Summary Compute Computed; Computed = Analysis.sum; Endcomp; Where does REPORT get the value for Analysis.sum ?
Detail Lines Answer: From the value of Analysis.sum on the same detail line. Detail 1 Detail 2 Detail 3 Summary Compute Computed; Computed = Analysis.sum; Endcomp;
Compute before/after Grouping Detail 1 Detail 2 Compute After Group1 Break after group1 /; Compute after Group1; Line ‘Compute After Group1. Val = ‘ analysis.sum 5.; Endcomp;
Compute before/after Grouping Detail 1 Detail 2 Compute After Group1 Break after group1 /; Compute after Group1; Line ‘Compute After Group1. Val = ‘ analysis.sum 5.; Endcomp; Where does REPORT get the value for Analysis.sum ?
Compute before/after Grouping Answer: From the summarized value of Analysis.sum for this grouping. NOTE: Summary is implicitly done by REPORT. Detail 1 Detail 2 Compute After Group1 Break after group1 /; Compute after Group1; Line ‘Compute After Group1. Val = ‘ analysis.sum 5.; Endcomp;
Compute after ; Detail 1 Detail 2 Detail 3 Report Summary RBREAK after /; Compute after; Line ‘Line Stmt after Report. Val = ‘ analysis.sum 5.; Endcomp;
Compute after ; Detail 1 Detail 2 Detail 3 Report Summary RBREAK after /; Compute after; Line ‘Line Stmt after Report. Val = ‘ analysis.sum 5.; Endcomp; Where does REPORT get the value for Analysis.sum ?
Compute after ; Answer: From the summarized value of Analysis.sum for this report. NOTE: Summary is implicitly done by REPORT. Detail 1 Detail 2 Detail 3 Report Summary RBREAK after /; Compute after; Line ‘Line Stmt after Report. Val = ‘ analysis.sum 5.; Endcomp;
“Dot” Syntax Analysis Variable . Statistic
Computed Columns Under Across Column dept quarter,(sales actual balance); Define balance / computed; Compute Balance; _c4_ = _c2_ - _c3_; _c7_ = _c5_ - _c6_; Endcomp;
Performance suggestion/trick • Sometimes it is best to create a dummy column and do your work in that column • Have the dummy column compute block write the values in the cells that are to its left • To make this work, the dummy column must come to the RIGHT of any column values you want to use • This dummy column is usually not printed – make it a NOPRINT column
Computed Columns Under Across Column dept quarter,(sales actual balance) dummy; Define balance / computed; Define dummy / noprint; Compute Dummy; _c4_ = _c2_ - _c3_; _c7_ = _c5_ - _c6_; Endcomp;
Computed Columns Don’t Summarize Detail 1 Detail 2 Detail 3 Summary Compute computedColumn; computedColumn = 5; Endcomp; Rbreak after / summarize;
Computed Columns Don’t Summarize Detail 1 Detail 2 Detail 3 Summary Compute computedColumn; If _break_ = ‘ ‘ then do; ComputedColumn = 5; total = total + ComputedColumn; End; Else computedColumn = total; Endcomp;
Summary • Uses of Compute Blocks • What do Compute Blocks look like? • Rules • Tricks/Traps