Question
Asked 11th Mar, 2018
  • St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)

Is there COUNTIFS function of excel in SPSS? If yes , please explain the syntax.?

Suppose the data for different households are like this
Household No. Sex1 Sex2 Sex3 Age1 Age2 Age3 feage<30
HHID1 1 1 2 24 22 2
HHID2 2 1 2 45 32 23
Here 1=male and 2=female in case of sex.
How can I find out the number of females of age less than 30 in households, the 8th column using SPSS syntax or SPSS menu?

Most recent answer

Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
Hi Bruce,
Thanks for so much help...to include the more than 50 years of females, femalemor50 variable was added to the syntax
COMPUTE femaleless26age = 0.
COMPUTE female26to50 = 0.
COMPUTE femalemor50 = 0.
DO REPEAT a = age.1 TO age.32 / s = sex.1 TO sex.32.
- COMPUTE femaleless26age = SUM(femaleless26age, (a LT 26)*(s EQ 2)).
- COMPUTE female26to50 = SUM(female26to50, RANGE(a,26,50)*(s EQ 2)).
- COMPUTE femalemor50 = SUM(femalemor50, (a GT 50)*(s EQ 2)).
END REPEAT.
FORMATS femaleless26age female26to50 femalemor50(F2.0).
LIST sex.1 to age.32 femaleless26age female26to50 femalemor50.
It s working well...
Thank you..
Tinu

All Answers (21)

John-Kåre Vederhus
Sørlandet Hospital
feage<30 is a dichotomous variable, right? Then you can do a cross table analysis with household to get counts. It's on the analysis - descriptive menu.
Best
Jk
Bruce Weaver
Lakehead University Thunder Bay Campus
I would try something like this (untested):
COMPUTE NumLT30 = SUM(Age1 LT 30, Age2 LT 30, Age3 LT 30).
FORMATS NumLT30 (F2.0).
VARIABLE LABELS NumLT30 "# of people aged < 30 years".
If you had a lot of variables to check, some kind of loop would be more efficient. E.g., if you had ages for up to 20 people, you could try something like this:
COMPUTE NumLT30 = 0.
DO REPEAT a = Age1 to Age20.
- COMPUTE NumLT30 = SUM(NumLT30, a LT 30).
END REPEAT.
HTH.
Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
@ John-Kåre Vederhus & Bruce Weaver , Thanx a lot. I am attaching a sample SPSS file which contains household id, sex and age of members in a four member three households. How can I make a single variable named 'femaleless30age' containing the number of females in households upto 30 years of age. In excel, this can be done with COUNTIFS function. Could you please provide the syntax for doing the same? Thank you.
John-Kåre Vederhus
Sørlandet Hospital
Hi Tinu
You use the 'Count values within cases' on the Transform menu.
You put the relevant variables in 'Numeric variables' box, and the click on 'Define variables' and specify a range (e.g., <=30, as in the syntax below)
This syntax seems to work fine:
COUNT Count_females_in_household=age1 age2 age3 age4(Lowest thru 30).
EXECUTE.
Best
JK
John-Kåre Vederhus
Sørlandet Hospital
I didn't notice that there were both men and women. There is also an "if" condition in the same dialogue box, try that. You may have to specify one person at a time and then compute a sum at the end.
jk
John-Kåre Vederhus
Sørlandet Hospital
Probably not very elegant but it seems to work:
(I assume that sex1 relates to Age1 and so on.....)
DO IF (sex1 = 2).
COUNT AGE_person_1=age1(Lowest thru 30).
END IF.
EXECUTE.
DO IF (sex2 = 2).
COUNT AGE_person_2=age2(Lowest thru 30).
END IF.
EXECUTE.
DO IF (sex3 = 2).
COUNT AGE_person_3=age3(Lowest thru 30).
END IF.
EXECUTE.
DO IF (sex4 = 2).
COUNT AGE_person_4=age4(Lowest thru 30).
END IF.
EXECUTE.
RECODE AGE_person_1 AGE_person_2 AGE_person_3 AGE_person_4 (SYSMIS=0).
EXECUTE. /* you have to recode sysmis = 0 before summing.
COMPUTE Count_female_30_rev=AGE_person_1 + AGE_person_2 + AGE_person_3 + AGE_person_4.
EXECUTE. /* Count females <=30.
Bruce Weaver
Lakehead University Thunder Bay Campus
Here's a variation on my DO-REPEAT suggestion.
* Version 1.
COMPUTE femaleless30age = 0.
DO REPEAT a = age1 TO age4 / s = sex1 TO sex4.
- COMPUTE femaleless30age = SUM(femaleless30age, (a LT 30)*(s EQ 2)).
END REPEAT.
FORMATS femaleless30age (F2.0).
LIST sex1 to age4 femaleless30age.
* Version 2: A few more keystrokes, but perhaps more transparent.
COMPUTE femaleless30age = 0.
DO REPEAT a = age1 TO age4 / s = sex1 TO sex4.
- COMPUTE #aLT30 = a LT 30. /* 1 if age < 30, 0 otherwise.
- COMPUTE #Female = s EQ 2. /* 1 if female, 0 otherwise.
- COMPUTE #BothTrue = #aLT30 AND #Female. /* Both conditions true.
- COMPUTE femaleless30age = SUM(femaleless30age, #BothTrue).
END REPEAT.
FORMATS femaleless30age (F2.0).
LIST sex1 to age4 femaleless30age.
1 Recommendation
Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
Thanx Bruce...though it s easy to work out but confusing.
COMPUTE femaleless30age = 0.
what s de need for putting equal to 0??
DO REPEAT a = age1 TO age4 / s = sex1 TO sex4.
Is it divided (/) by s???
COMPUTE femaleless30age = SUM(femaleless30age, (a LT 30)*(s EQ 2))
Is we r multiplying (a LT 30)*(s EQ 2)??? because there is * sign in between??.
It worked & easy but littled confusing.
@ John-Kåre Vederhus ,,,,Thank you...easy to understand u r explanations. But I have 32 variables for each factors.
such as sex1, sex2, sex3...sex32 + age1, age2,age3.....age32 + occup1, occup2, occup3...occup32.
so the syntax would be little longer. But still hope I can follow this...
Warm Regds..
John-Kåre Vederhus
Sørlandet Hospital
OK, large families, it seems. I still think you can follow the same procedures as described above (both Bruce and mine will work, you only have to revise them to include the other family members).
Best
john-kåre
Bruce Weaver
Lakehead University Thunder Bay Campus
Tinu, you can read up on DO-REPEAT via the following links.
I initialized femaleless30age to 0 before the DO-REPEAT structure, because if there are no females who meet the age requirement, you want that variable to equal 0.
John-Kåre Vederhus
Sørlandet Hospital
I'll try, Bruce can correct me if I'm wrong:
Bruce first computed the femaleless30 variable and put in the "0"
He then defined "a" as age and "s" as sex and described the conditions:
- a = LT 30 must mean age less than 30
- s EQ 2 means sex equals "2" = women (your value for women, "1" was men)
He then set up the repeat code (choose those who meet the requirement in both condition a and s)
At the end, the sum of those who satisfied the condition above was added to the femaleless30 variable
[ - COMPUTE femaleless30age = SUM(femaleless30age, (a LT 30)*(s EQ 2)). ]
FORMATS femaleless30age (F2.0). means just a standard numeric variables without decimals?
Right, @Bruce Weaver ?
Best
jk
Bruce Weaver
Lakehead University Thunder Bay Campus
Correct, John-Kåre. Thanks.
Maybe this table will help further. You'll likely have to copy and paste into a text editor or Word Processor where you can use a fixed font (e.g., Courier) to make things align properly.
Age Sex (a LT 30) (s EQ 2) (a LT 30)*(s EQ 2)
----------------------------------------------------
29 F (2) TRUE(1) TRUE(1) 1
29 M (1) TRUE(1) FALSE(0) 0
30 F (2) FALSE(0) TRUE(1) 0
30 M (1) FALSE(0) FALSE(0) 0
Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
Dear John & Bruce..thanks a lot...your suggestions are working well...
Thank you,
Regds,
Tinu
Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
Hi Bruce & John,
For finding female less 30 age we used this formulae
- a = LT 30 must mean age less than 30
But for finding female between 30 and 50, could you please tell the formulae.
I tried the following syntax:
COMPUTE female50age = 0.
DO REPEAT a = age.1 TO age.32 / s = sex.1 TO sex.32.
- COMPUTE #a26thru50 = (a >25<51). /* 1 if age >25 <51, 0 otherwise.
- COMPUTE #Female = s EQ 2. /* 1 if female, 0 otherwise.
- COMPUTE #BothTrue =#a26thru50 AND #Female. /* Both conditions true.
- COMPUTE female50age = SUM(female50age, #BothTrue).
END REPEAT.
FORMATS female50age (F2.0).
LIST sex.1 to age.32 female50age.
But not getting correct answer. Could you please help?
Regds,
Tinu
Bruce Weaver
Lakehead University Thunder Bay Campus
Hi Tinu. Try this (untested). The added bits are in bold face.
* ---- Start of syntax ----.
COMPUTE femaleless30age = 0.
COMPUTE female30to50 = 0.
DO REPEAT a = age1 TO age4 / s = sex1 TO sex4.
- COMPUTE femaleless30age = SUM(femaleless30age, (a LT 30)*(s EQ 2)).
* NOTE: first version had 'age' where it should have been 'a'.
- COMPUTE female30to50 = SUM(female30to50, RANGE(a,30,50)*(s EQ 2)).
END REPEAT.
FORMATS femaleless30age female30to50 (F2.0).
LIST sex1 to age4 femaleless30age female30to50.
* ---- End of syntax ----.
RANGE(age,30,50) returns TRUE (i.e., 1) if age is within the range 30 to 50. Otherwise it returns FALSE (i.e., 0).
HTH.
1 Recommendation
Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
Dear Bruce,
Sorry, I am not getting the answer.
Sir,
For finding the females aged below 30 years, the following syntax was suggested by you and it worked well. Now I need to find the females between 30 and 50 years...could you please send the syntax. Thnx.
COMPUTE femaleless30age = 0.
DO REPEAT a = age1 TO age4 / s = sex1 TO sex4.
- COMPUTE #aLT30 = a LT 30. /* 1 if age < 30, 0 otherwise.
- COMPUTE #Female = s EQ 2. /* 1 if female, 0 otherwise.
- COMPUTE #BothTrue = #aLT30 AND #Female. /* Both conditions true.
- COMPUTE femaleless30age = SUM(femaleless30age, #BothTrue).
END REPEAT.
FORMATS femaleless30age (F2.0).
LIST sex1 to age4 femaleless30age.
Bruce Weaver
Lakehead University Thunder Bay Campus
Hi Tinu. One of the COMPUTE lines had 'age' where it should have had 'a'. I have edited the earlier post to fix the code. HTH.
Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
Great ..Bruce...
Thanks a lot Sir...it s working. I want to know one more thing. The last two sentences in the syntax "
RANGE(age,30,50) returns TRUE (i.e., 1) if age is within the range 30 to 50. Otherwise it returns FALSE (i.e., 0).
HTH.""
Is these also part of syntax or just explanation.
Thanks a lot.
Tinu
Bruce Weaver
Lakehead University Thunder Bay Campus
Hi Tinu. That part is explanatory. I've done another edit to the post containing the syntax, adding comment lines to indicate the start and end of the syntax.
But note too that in your actual dataset, which probably has many more cases than the sample file you uploaded, you probably do not want to include the LIST command at the end. That will list all of the cases in your data file to the output window! It's useful when using small datasets to work out and demonstrate a solution, but nobody wants a listing like when working with a large dataset.
Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
Hi Bruce,
Thanks for so much help...to include the more than 50 years of females, femalemor50 variable was added to the syntax
COMPUTE femaleless26age = 0.
COMPUTE female26to50 = 0.
COMPUTE femalemor50 = 0.
DO REPEAT a = age.1 TO age.32 / s = sex.1 TO sex.32.
- COMPUTE femaleless26age = SUM(femaleless26age, (a LT 26)*(s EQ 2)).
- COMPUTE female26to50 = SUM(female26to50, RANGE(a,26,50)*(s EQ 2)).
- COMPUTE femalemor50 = SUM(femalemor50, (a GT 50)*(s EQ 2)).
END REPEAT.
FORMATS femaleless26age female26to50 femalemor50(F2.0).
LIST sex.1 to age.32 femaleless26age female26to50 femalemor50.
It s working well...
Thank you..
Tinu
Tinu Joseph
St. Francis De Sales' College (Rashtrasant Tukadoji Maharaj Nagpur University)
Hi Bruce,
Thanks for so much help...to include the more than 50 years of females, femalemor50 variable was added to the syntax
COMPUTE femaleless26age = 0.
COMPUTE female26to50 = 0.
COMPUTE femalemor50 = 0.
DO REPEAT a = age.1 TO age.32 / s = sex.1 TO sex.32.
- COMPUTE femaleless26age = SUM(femaleless26age, (a LT 26)*(s EQ 2)).
- COMPUTE female26to50 = SUM(female26to50, RANGE(a,26,50)*(s EQ 2)).
- COMPUTE femalemor50 = SUM(femalemor50, (a GT 50)*(s EQ 2)).
END REPEAT.
FORMATS femaleless26age female26to50 femalemor50(F2.0).
LIST sex.1 to age.32 femaleless26age female26to50 femalemor50.
It s working well...
Thank you..
Tinu

Similar questions and discussions

Related Publications

Presentation
Full-text available
Abstract [https://surveyresearch.weebly.com/old-dog-old-tricks-using-spss-syntax-to-beat-the-mouse-trap.html] The presentation draws on my experiences: first, of using SPSS (from 1972 to 1993, and on a range of mainframe computers) to process and analyse dozens of surveys; second, of training several hundred researchers and students in the use of S...
Book
If you'd like to buy this course in online pre-recorded form, along with the coursebook and the course materials, for 75 UK pounds (50 for students) then contact me on c.b.stride@shef.ac.uk or go to www.figureitout.org.uk to purchase
Got a technical question?
Get high-quality answers from experts.