Percent Change for Multiple Variables

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

Percent Change for Multiple Variables

DKUKEC
** Dear SPSSers,

The following was adapted using syntax developed by Art Kendal.  It is very useful for quickly computing percent change and overall change between the earliest year and the most recent year for one variable.  
What I was wondering is can the syntax be further refined to compute multiple percentage changes for each of the variable without having to repeat the syntax and rename the target variables in each case?  
The syntax would compute percent change for each variable and then the "aggfile" would compute the total changes for each variable as well - adding the variable name in each row.  

Please let me know if you have any questions concerning my question and thank you in advance for your assistance.

Sincerely
Damir


* /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  .

DATA LIST LIST / YEAR (F4) TOTALARREST (F6) PART1 (F6) PART2 (F6) POPULATION (F6) TOTALARRESTRATE PART1_R PART2_R .
BEGIN DATA  
1998 54573 10909 43664 1020521 5347.6 1069.0 4278.6
1999 63322 10555 52767 1042196 6075.8 1012.8 5063.1
2000 68363 9810 58553 1131184 6043.5 867.2 5176.3
2001 71553 11096 60457 1154464 6197.9 961.1 5236.8
2002 69409 11262 58147 1183197 5866.2 951.8 4914.4
2003 72381 10939 61442 1211448 5974.8 903.0 5071.8
2004 71054 10525 60529 1242270 5719.7 847.2 4872.5
2005 75031 10409 64622 1265900 5927.1 822.3 5104.8
2006 73424 10504 62920 1287987 5700.7 815.5 4885.1
2007 81743 11676 70067 1295033 6312.0 901.6 5410.4
2008 85981 12902 73079 1294654 6641.2 996.6 5644.7
2009 74337 12723 61614 1287344 5774.4 988.3 4786.1
2010 70479 11146 59333 1286461 5478.5 866.4 4612.1
2011 62530 10774 51756 1325758 4716.5 812.7 3903.9
2012 60625 10880 49745 1335415 4539.8 814.7 3725.1
END DATA .
DATASET NAME CRIME .

COMPUTE arrestchange=( totalarrest / lag(totalarrest) - 1 )*100.
FORMATS arrestchange (pct9.2).
VARIABLE LABELS ARRESTCHANGE 'year to year pct change'.
LIST .

COMPUTE nobreak = 1.
DATASET DECLARE aggfile.
AGGREGATE OUTFILE=aggfile /break = nobreak
    /entries = n
    /start   = first(totalarrest)
    /end     = last (totalarrest).
DATASET ACTIVATE aggfile.
COMPUTE periods= entries-1.
COMPUTE grosschange= ( (end /start)-1) * 100.
COMPUTE avgchange = grosschange/periods.
COMPUTE growthrate  =((end/start) **(1/periods))-1.
COMPUTE annualgrowthpct = 100*growthrate.
FORMATS grosschange avgchange annualgrowthpct (pct7.2) growthrate (f8.5).
COMPUTE check = start*((1+growthrate)**periods).
FORMATS check (f6).
LIST .

Reply | Threaded
Open this post in threaded view
|

Re: Percent Change for Multiple Variables

David Marso
Administrator
See VARSTOCASES command (INDEX subcommand) and BREAK subcommand in AGGREGATE.
DKUKEC wrote
** Dear SPSSers,

The following was adapted using syntax developed by Art Kendal.  It is very useful for quickly computing percent change and overall change between the earliest year and the most recent year for one variable.  
What I was wondering is can the syntax be further refined to compute multiple percentage changes for each of the variable without having to repeat the syntax and rename the target variables in each case?  
The syntax would compute percent change for each variable and then the "aggfile" would compute the total changes for each variable as well - adding the variable name in each row.  

Please let me know if you have any questions concerning my question and thank you in advance for your assistance.

Sincerely
Damir


* /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  .

DATA LIST LIST / YEAR (F4) TOTALARREST (F6) PART1 (F6) PART2 (F6) POPULATION (F6) TOTALARRESTRATE PART1_R PART2_R .
BEGIN DATA  
1998 54573 10909 43664 1020521 5347.6 1069.0 4278.6
1999 63322 10555 52767 1042196 6075.8 1012.8 5063.1
2000 68363 9810 58553 1131184 6043.5 867.2 5176.3
2001 71553 11096 60457 1154464 6197.9 961.1 5236.8
2002 69409 11262 58147 1183197 5866.2 951.8 4914.4
2003 72381 10939 61442 1211448 5974.8 903.0 5071.8
2004 71054 10525 60529 1242270 5719.7 847.2 4872.5
2005 75031 10409 64622 1265900 5927.1 822.3 5104.8
2006 73424 10504 62920 1287987 5700.7 815.5 4885.1
2007 81743 11676 70067 1295033 6312.0 901.6 5410.4
2008 85981 12902 73079 1294654 6641.2 996.6 5644.7
2009 74337 12723 61614 1287344 5774.4 988.3 4786.1
2010 70479 11146 59333 1286461 5478.5 866.4 4612.1
2011 62530 10774 51756 1325758 4716.5 812.7 3903.9
2012 60625 10880 49745 1335415 4539.8 814.7 3725.1
END DATA .
DATASET NAME CRIME .

COMPUTE arrestchange=( totalarrest / lag(totalarrest) - 1 )*100.
FORMATS arrestchange (pct9.2).
VARIABLE LABELS ARRESTCHANGE 'year to year pct change'.
LIST .

COMPUTE nobreak = 1.
DATASET DECLARE aggfile.
AGGREGATE OUTFILE=aggfile /break = nobreak
    /entries = n
    /start   = first(totalarrest)
    /end     = last (totalarrest).
DATASET ACTIVATE aggfile.
COMPUTE periods= entries-1.
COMPUTE grosschange= ( (end /start)-1) * 100.
COMPUTE avgchange = grosschange/periods.
COMPUTE growthrate  =((end/start) **(1/periods))-1.
COMPUTE annualgrowthpct = 100*growthrate.
FORMATS grosschange avgchange annualgrowthpct (pct7.2) growthrate (f8.5).
COMPUTE check = start*((1+growthrate)**periods).
FORMATS check (f6).
LIST .
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Percent Change for Multiple Variables

Richard Ristow
The original posting doesn't seem to have hit SPSSX-L yet; come on, Nabble!

Anyway, at 10:33 AM 2/12/2014, David Marso quoted DKUKEC:

>The following was adapted using syntax developed by Art Kendal.  It
>is very useful for quickly computing percent change and overall
>change between the earliest year and the most recent year for one
>variable. Can the syntax be further refined to compute multiple
>percentage changes for each of the variable without having to repeat
>the syntax and rename the target variables in each case?

First, are you thinking of this for year-to-year change?

>COMPUTE arrestchange=( totalarrest / lag(totalarrest) - 1 )*100.
>FORMATS arrestchange (pct9.2).
>VARIABLE LABELS ARRESTCHANGE 'year to year pct change'.

You don't nearly have to repeat the whole thing. Making a guess what
variables you want to compute change for, you could write (code not tested),

DO REPEAT    Value = TOTALARREST  PART1 PART2 POPULATION
             /Change= ARRESTCHANGE PT1CH PT2CH POPCHANGE.
.  COMPUTE Change =( Value / lag(Value) - 1 )*100.
END REPEAT.
FORMATS ARRESTCHANGE PT1CH PT2CH POPCHANGE (pct9.2).

For the change from earliest to latest values, you can name any
number of summary variables on the AGGREGATE:

AGGREGATE OUTFILE=aggfile /break = nobreak
     /entries  = n
     /A_Arrest = first(totalarrest)
     /Z_Arrest = last (totalarrest)
     /A_Part1  = first(PART1)
     /Z_Part1  = last (PART1)
     ...

Then, in the aggregated dataset (DATASET ACTIVATE aggfile) you can
define and compute the 'change' variable for each of your underlying
variables; again, DO REPEAT can keep you from having to repeat the
code again and again.

Alternatively, you could do what David Marso suggested, and 'unroll'
your data so each variable is in a separate group of cases. Then the
calculations need only be done once. However, re-assembling into
'wide' form can be a bear. (Code, as always, is in an APPENDIX at the
end of this posting.)
|-----------------------------|---------------------------|
|Output Created               |12-FEB-2014 17:55:57       |
|-----------------------------|---------------------------|
  [CRIME]
YEAR TOTALARREST  PART1  PART2 POPULATION TOTALARRESTRATE  PART1_R  PART2_R

1998     54573    10909  43664   1E+006        5347.60     1069.00  4278.60
1999     63322    10555  52767   1E+006        6075.80     1012.80  5063.10
2000     68363     9810  58553   1E+006        6043.50      867.20  5176.30
2001     71553    11096  60457   1E+006        6197.90      961.10  5236.80
2002     69409    11262  58147   1E+006        5866.20      951.80  4914.40
2003     72381    10939  61442   1E+006        5974.80      903.00  5071.80
2004     71054    10525  60529   1E+006        5719.70      847.20  4872.50
2005     75031    10409  64622   1E+006        5927.10      822.30  5104.80
2006     73424    10504  62920   1E+006        5700.70      815.50  4885.10
2007     81743    11676  70067   1E+006        6312.00      901.60  5410.40
2008     85981    12902  73079   1E+006        6641.20      996.60  5644.70
2009     74337    12723  61614   1E+006        5774.40      988.30  4786.10
2010     70479    11146  59333   1E+006        5478.50      866.40  4612.10
2011     62530    10774  51756   1E+006        4716.50      812.70  3903.90
2012     60625    10880  49745   1E+006        4539.80      814.70  3725.10

Number of cases read:  15    Number of cases listed:  15


DATASET COPY     Unroll.
DATASET ACTIVATE Unroll.
VARSTOCASES /ID = id
  /MAKE Value
   FROM TOTALARREST PART1   PART2 POPULATION
        TOTALARRESTRATE  PART1_R PART2_R
  /INDEX = Variable(Value)
  /KEEP = YEAR
  /NULL = KEEP.

Variables to Cases
|-----------------------------|---------------------------|
|Output Created               |12-FEB-2014  17:55:57      |
|-----------------------------|---------------------------|
  [Unroll]
Generated Variables
|--------|------|
|Name    |Label |
|--------|------|
|id      |<none>|
|Variable|<none>|
|Value   |<none>|
|--------|------|

Processing Statistics
|-------------|-|
|Variables In |8|
|Variables Out|4|
|-------------|-|

SORT CASES BY Variable YEAR.
FORMATS Value (F9.2).
LIST.
List
|-----------------------------|---------------------------|
|Output Created               |12-FEB-2014 17:55:57       |
|-----------------------------|---------------------------|
  [Unroll]
       id YEAR Variable            Value

        1 1998 PART1            10909.00
        2 1999 PART1            10555.00
        3 2000 PART1             9810.00
        4 2001 PART1            11096.00
        5 2002 PART1            11262.00
        6 2003 PART1            10939.00
        7 2004 PART1            10525.00
        8 2005 PART1            10409.00
        9 2006 PART1            10504.00
       10 2007 PART1            11676.00
       11 2008 PART1            12902.00
       12 2009 PART1            12723.00
       13 2010 PART1            11146.00
       14 2011 PART1            10774.00
       15 2012 PART1            10880.00
        1 1998 PART1_R           1069.00
        2 1999 PART1_R           1012.80
        3 2000 PART1_R            867.20
        4 2001 PART1_R            961.10
        5 2002 PART1_R            951.80
        6 2003 PART1_R            903.00
        7 2004 PART1_R            847.20
        8 2005 PART1_R            822.30
        9 2006 PART1_R            815.50
       10 2007 PART1_R            901.60
       11 2008 PART1_R            996.60
       12 2009 PART1_R            988.30
       13 2010 PART1_R            866.40
       14 2011 PART1_R            812.70
       15 2012 PART1_R            814.70
        1 1998 PART2            43664.00
        2 1999 PART2            52767.00
...
  Number of cases read:  105    Number of cases listed:  105
=============================
APPENDIX: Test data, and code
(not saved separately)
=============================
DATA LIST LIST / YEAR (F4) TOTALARREST (F6) PART1 (F6) PART2 (F6)
POPULATION (F6) TOTALARRESTRATE PART1_R PART2_R .
BEGIN DATA
1998  54573   10909   43664   1020521 5347.6  1069.0  4278.6
1999  63322   10555   52767   1042196 6075.8  1012.8  5063.1
2000  68363   9810    58553   1131184 6043.5  867.2   5176.3
2001  71553   11096   60457   1154464 6197.9  961.1   5236.8
2002  69409   11262   58147   1183197 5866.2  951.8   4914.4
2003  72381   10939   61442   1211448 5974.8  903.0   5071.8
2004  71054   10525   60529   1242270 5719.7  847.2   4872.5
2005  75031   10409   64622   1265900 5927.1  822.3   5104.8
2006  73424   10504   62920   1287987 5700.7  815.5   4885.1
2007  81743   11676   70067   1295033 6312.0  901.6   5410.4
2008  85981   12902   73079   1294654 6641.2  996.6   5644.7
2009  74337   12723   61614   1287344 5774.4  988.3   4786.1
2010  70479   11146   59333   1286461 5478.5  866.4   4612.1
2011  62530   10774   51756   1325758 4716.5  812.7   3903.9
2012  60625   10880   49745   1335415 4539.8  814.7   3725.1
END DATA .
DATASET NAME CRIME .

LIST.
DATASET ACTIVATE Crime

DATASET COPY     Unroll.
DATASET ACTIVATE Unroll.

VARSTOCASES /ID = id
  /MAKE Value
   FROM TOTALARREST PART1   PART2 POPULATION
        TOTALARRESTRATE  PART1_R PART2_R
  /INDEX = Variable(Value)
  /KEEP = YEAR
  /NULL = KEEP.

SORT CASES BY Variable YEAR.

FORMATS Value (F9.2).
LIST.

=====================
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