Counting a range of values within each group

Viewed 157

I have a dataset where each observation/row is a person belonging to a certain family.

There is a variable "Family_ID" that for each member of the same family has the same number.

Moreover, there is variable "Age" and according to this one I have to create a new variable which for each family it counts the number of members aged 0-13 and assigns this number to each member of the family. I manually created the variable "Number0_13" to give you the idea of I'm trying to do.

How can I do this??

Thanks in advance for your help

Family_ID Age Number0_13
1         40    2
1         38    2
1         5     2
1         10    2
2         43    1
2         39    1
2         12    1
3         25    1  
3         24    1  
3         1     1  
1 Answers

You can use the AGGREGATE function to do that. You specify which variable to group by setting /BREAK and then create your aggregated variables using Aggregate Functions.

In this case, CIN() counts the number of cases (for each Family_ID) that have an Age between 0 and 13 (inclusive).

AGGREGATE
 /OUTFILE=* MODE=ADDVARIABLES
 /BREAK=Family_ID
 /Number0_13=CIN(Age, 0, 13) .

Note that we could've also used CLT(Age, 14) in this case.

Related