Hello,
I hope I'm explaining this correctly... So, if a variable name is "Animal," with 2 labels: 1 - "Dogs" 2 - "Cats". I want to compute a new variable called "Pets" that is equal to the frequency count of "Dogs." So if there are 55 cases of "Dogs," then all values of "Pets" would equal 55. Of course, it'd be easy to just freq animal and then compute pets = the frequency count of dogs. But I'm looking for something that could automate it more...something like a "Count Pets = Animal(Dogs)" (but all the values would equal the count of dogs). Hope someone can help, and many thanks! |
At 03:40 PM 2/9/2015, CJLee wrote:
>So, if a variable name is "Animal," with 2 labels: 1 - "Dogs" 2 - "Cats". > >I want to compute a new variable called "Pets" that is equal to the >frequency count of "Dogs." So if there are 55 cases of "Dogs," then >all values of "Pets" would equal 55. It's a straightforward AGGREGATE -- though I'll call the variable "TotlDogs" instead of "Pets": COMPUTE TotlDogs = (Animal EQ 1). VAR LABEL TotlDogs 'Total number of dogs noted in the file'. AGGREGATE OUTFILE=* MODE=ADDVARIABLES /TotlDogs = SUM(TotlDogs). FORMATS TotlDogs (F4). For SPSS releases 13-16, you need to compute and use a variable "NoBreak": COMPUTE NoBreak = 1 /* In SPSS 13-16 */. COMPUTE TotlDogs = (Animal EQ 1). VAR LABEL TotlDogs 'Total number of dogs noted in the file'. AGGREGATE OUTFILE=* MODE=ADDVARIABLES /BREAK = NoBreak /* In SPSS 13-16 */. /TotlDogs = SUM(TotlDogs). FORMATS TotlDogs (F4). It's a little more complicated in releases 13 and earlier, but I'll skip that unless it affects you. ===================== 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 |
Thank you very much!
Works perfectly, and so simple! |
Richard, don't you need an OVERWRITE=YES in the AGGREGATE commands? I wasn't aware that you could now use AGGREGATE without a BREAK sub-command, that helps! On 9 February 2015 at 21:36, CJLee <[hidden email]> wrote: Thank you very much! |
In reply to this post by CJLee
Correction to previous post (with thanks); at 01:44 AM 2/10/2015,
Jignesh Sutar wrote: >Richard, don't you need an OVERWRITE=YES in the AGGREGATE commands? Yes, you do. Corrected (and tested) code follows. >I wasn't aware that you could now use AGGREGATE without a BREAK >sub-command, that helps! Nice, isn't it? Added in SPSS 17. SPSS 17 and later: COMPUTE TotlDogs = (Animal EQ 1). VAR LABEL TotlDogs 'Total number of dogs noted in the file'. AGGREGATE OUTFILE=* MODE=ADDVARIABLES OVERWRITE=YES /TotlDogs = SUM(TotlDogs). FORMATS TotlDogs (F4). SPSS 13-16: COMPUTE NoBreak = 1 /* In SPSS 13-16 */. COMPUTE TotlDogs = (Animal EQ 1). VAR LABEL TotlDogs 'Total number of dogs noted in the file'. AGGREGATE OUTFILE=* MODE=ADDVARIABLES OVERWRITE=YES /BREAK = NoBreak /* In SPSS 13-16 */ /TotlDogs = SUM(TotlDogs). FORMATS TotlDogs (F4). ============================ APPENDIX: Test data and code (Run under SPSS 14) ============================ NEW FILE. INPUT PROGRAM. . NUMERIC CaseId (N3) Animal (F2). . VAR LABEL Animal 'Animal owned by the household'. . VAL LABEL Animal 1 'Dogs' 2 'Cats' 0 'None'. . LOOP CaseId = 1 TO 100. . COMPUTE Animal = TRUNC(RV.UNIFORM(0,3)). . END CASE. . END LOOP. END FILE. END INPUT PROGRAM. FREQUENCIES Animal. COMPUTE NoBreak = 1 /* In SPSS 13-16 */. COMPUTE TotlDogs = (Animal EQ 1). VAR LABEL TotlDogs 'Total number of dogs noted in the file'. AGGREGATE OUTFILE=* MODE=ADDVARIABLES OVERWRITE=YES /BREAK = NoBreak /* In SPSS 13-16 */ /TotlDogs = SUM(TotlDogs). FORMATS TotlDogs (F4). ===================== 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 |
Free forum by Nabble | Edit this page |