• <GetStart>
  • CSPro User's Guide
    • The CSPro System
    • Data Dictionary Module
    • The CSPro Language
    • Data Entry Module
    • Batch Editing Applications
    • Tabulation Applications
      • Introduction to Tabulation
      • Parts of a Table
      • Parts of the Table Tree
      • Common Uses of Tabulation
      • Capabilities of Tabulation
      • Creating Tables
      • Formatting Tables
      • Creating Tables by Geographic Area
      • Printing Tables
      • Tabulation Preferences
      • Saving and Copying Table Data
      • Table Post Calculation
        • Introduction to Table Post Calculation
        • Adding Rows and Columns For Post Calculation
        • Post Calculation For Individual Cells
        • Post Calculation For Rows, Columns and Ranges
        • Row and Column Indexing for Post Calculation
      • Run Production Tabulations
      • Advanced Table Topics
      • Table Tips and Tricks
    • Data Sources
    • CSPro Statements and Functions
    • Templated Reporting System
    • HTML and JavaScript Integration
    • Action Invoker
    • Appendix
  • <CSEntry>
  • <CSBatch>
  • <CSTab>
  • <DataViewer>
  • <TextView>
  • <TblView>
  • <CSFreq>
  • <CSDeploy>
  • <CSPack>
  • <CSDiff>
  • <CSConcat>
  • <Excel2CSPro>
  • <CSExport>
  • <CSIndex>
  • <CSReFmt>
  • <CSSort>
  • <ParadataConcat>
  • <ParadataViewer>
  • <CSCode>
  • <CSDocument>
  • <CSView>
  • <CSWeb>

Post Calculation For Rows, Columns and Ranges

In addition to performing operations on individual cells, you can perform operations on cell ranges, rows and columns. As an example, take the following table, in which an additional column is added to calculate the male to female ratio for each group.
For information on how to add the additional column, see the previous section: Adding Rows and Columns For Post Calculation.
The postcalc logic for assigning rows and columns is similar to that of individual cells, however there is a simplified syntax for working with cell ranges, rows and columns. You could set each cell individually with multiple statements as follows:
TABLE1(0, 3) = TABLE1(0, 1) / TABLE1(0, 2);
TABLE1(
1, 3) = TABLE1(1, 1) / TABLE1(1, 2);
// ...
TABLE1(2, 3) = TABLE1(2, 1) / TABLE1(2, 2);
TABLE1(
20, 3) = TABLE1(20, 1) / TABLE1(20, 2);
However, that would take a lot of code. Instead, you can specify this one statement using a range or a wild card. To use a range, give the lower and upper limits separated by a colon:
TABLE1[0:20, 3] = TABLE1[0:20, 1] / TABLE1[0:20, 2];
This means divide the cell in column 1 by the cell in column 2 and put the result in the cell in column 3 for each row from 0 to 20. You can also use a wildcard (an asterisk) to specify an entire row or column:
TABLE1[*, 3] = TABLE1[*, 1] / TABLE1[*, 2];
Using an asterisk in place of the row index means that the operation applies to all rows in the table. In this case it is the same as specifying the range 0:20 since the table has 21 rows. An asterisk can also be used in place of the column index to specify every column in a table:
TABLE1[1, *] = TABLE1[2, *]; // copy row 2 into row 1
Note that when working with rows, columns and ranges, you must use square brackets "[ ]" rather than parentheses "( )". Parentheses may only be used when specifying individual cells.
You can only assign ranges or wildcards to each other if the dimensions of the ranges match. For example:
TABLE1[0:2, 0:3] = TABLE1[3:5, 0:3];
This copies one 3 by 4 region of the table to another 3 by 4 region. However, the following code will fail since it attempts to copy a 3 by 4 region to a 3 by 3 region:
TABLE1[0:2, 0:2] = TABLE1[3:5, 0:3]; // This does not work !
See also: Post Calculation For Individual Cells, Row and Column Indexing for Post Calculation