Cumulated percentage in CTABLES

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Cumulated percentage in CTABLES

spss.giesel@yahoo.de
Dear SPSS users,

  I wonder if there is a built-in possibility to show cumulated percentages within CTABLES, like FREQUENCIES command does by default.
I know you can post compute them with PCOMPUTE but that's cumbersome if you have a variable with a wide range like numeric age or sth.
Any ideas are welcome.

Thanks,
  Mario
 
Mario Giesel
Munich, Germany
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD
Reply | Threaded
Open this post in threaded view
|

Re: Cumulated percentage in CTABLES

Jon Peck
Here is an example of how to do this with the STATS TABLE CALC extension command using employee data.sav, which is shipped with Statistics.

* A table with 3 category columns:
CTABLES
  /TABLE educ [C] BY jobcat [C][COUNT  COLPCT.COUNT PCT40.1]
  /CATEGORIES VARIABLES=educ ORDER=A KEY=VALUE EMPTY=INCLUDE TOTAL=YES POSITION=AFTER 
    MISSING=EXCLUDE
  /CATEGORIES VARIABLES=jobcat ORDER=A KEY=VALUE EMPTY=INCLUDE MISSING=EXCLUDE.

I assume that the cumulative percents column should be added next to the regular percents, and a total may or may not be at the bottom.  This will compute the cumulative percents column for each category of the column variable and label it "Cum Pct".  It uses a simple custom module, pcts.py, that appears below.  As it iterates over the rows, it calls the custom function cumpct in the pcts.py module.

STATS TABLE CALC SUBTYPE="customtable" PROCESS=PRECEDING 
/TARGET FORMULA="pcts.cumpct(arg, x[0])" DIMENSION=COLUMNS LEVEL = -1  LOCATION="Column N %" 
    REPEATLOC=YES LABEL="Cum Pct" MODE=AFTER HIDEINPUTS=NO CUSTOMMODULE="pcts"
/FORMAT CELLFORMAT="##.#%" DECIMALS=1  INVALID="".

Here is the custom module pcts.py.  Note that the indentation matters.

def cumpct(arg, curvalue):
    roworcol = arg['roworcol']
      
    if roworcol == 0:
        arg['cumvalue'] = curvalue
    else:
        arg['cumvalue'] = arg['cumvalue'] + curvalue

    return min(arg['cumvalue'], 100.)    # prevents a total row from getting added in

pcts.py should be saved where Python can find it such as in the python\lib\site-packages directory under your Statistics installation.  (I will send this as a file separately to Mario.)





On Tue, Jun 26, 2018 at 1:33 AM Mario Giesel <[hidden email]> wrote:
Dear SPSS users,

  I wonder if there is a built-in possibility to show cumulated percentages within CTABLES, like FREQUENCIES command does by default.
I know you can post compute them with PCOMPUTE but that's cumbersome if you have a variable with a wide range like numeric age or sth.
Any ideas are welcome.

Thanks,
  Mario
 
Mario Giesel
Munich, Germany
===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD


--
Jon K Peck
[hidden email]

===================== To manage your subscription to SPSSX-L, send a message to [hidden email] (not to SPSSX-L), with no body text except the command. To leave the list, send the command SIGNOFF SPSSX-L For a list of commands to manage subscriptions, send the command INFO REFCARD