Margaret Nozaki
Margaret Nozaki

Reputation: 1

How to create a variable from count of instances of an existing variable in SPSS

I want to create a variable that is the count of instances of a certain year in my dataset in SPSS. To be more specific, I am analyzing the number of suicides before and after 1996. I have a data set of the number of suicides each year from 1989-1998 (each data point is an individual suicide) and want to create a variable that's # of suicides each year. So, the best way I understand to do this is to create a variable that is the sum of instances of each year in my existing data set. Basically, I want to create a variable that is the total count of suicides when year=1989, another when year=1900, etc. from 1989-1998.

Here is a screenshot of the data view for context.

GOAL: create a new variable representing annual number of suicide for each year in the existing data set

I have tried the aggregate, compute variable, and select if functions, but none have worked. However, I could totally be using these incorrectly.

Thank you

Upvotes: 0

Views: 24

Answers (1)

eli-k
eli-k

Reputation: 11360

To add a new variable with the number of rows per year you can do this:

aggregate out=* mode=addvariables /break=year /NperYear=n.

The aggregate function counts the rows per year and mode=addvariables attaches the results to the existing dataset.

Upvotes: 1

Related Questions