|
Dear all,
<input onclick="jsCall();" id="jsProxy" type="hidden">I need to count the total number of cases in 30 columns of children that are aged 3 to 18 years old. I have the following: COUNT age3to18frequency=B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 B5_AGE$06 B5_AGE$07 B5_AGE$08 B5_AGE$09 B5_AGE$10 B5_AGE$11 B5_AGE$12 B5_AGE$13 B5_AGE$14 B5_AGE$15 B5_AGE$16 B5_AGE$17 B5_AGE$18 B5_AGE$19 B5_AGE$20 B5_AGE$21 B5_AGE$22 B5_AGE$23 B5_AGE$24 B5_AGE$25 B5_AGE$26 B5_AGE$27 B5_AGE$28 B5_AGE$29 B5_AGE$30(3 thru 18). EXECUTE. But now I want to count the total number of cases in the 30 columns of those children that are aged 3 to 18 AND are going to school. The other variable is currently going to school= B8- 1= yes, 2=no, SO if the variable for going to School is B8#1, B8#2, etc to B8#30, where do I put the DO If statement in? Thanks all, Deepa |
|
* if you want 30 counts and a grand total. numeric totalinschool (f5). do repeat b5 = B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 B5_AGE$06 B5_AGE$07 B5_AGE$08 B5_AGE$09 B5_AGE$10 B5_AGE$11 B5_AGE$12 B5_AGE$13 B5_AGE$14 B5_AGE$15 B5_AGE$16 B5_AGE$17 B5_AGE$18 B5_AGE$19 B5_AGE$20 B5_AGE$21 B5_AGE$22 B5_AGE$23 B5_AGE$24 B5_AGE$25 B5_AGE$26 B5_AGE$27 B5_AGE$28 B5_AGE$29 B5_AGE$30/ b8 = B8#1 to B8#30/ inschool = inschool1 to inschool30. if b8 eq 1 and range(b5,3,18) inschool = 1. end repeat. compute totalinschool = sum.1(inschool1 to inschool30). if you do not need all 30 counts something like this. numeric totalinschool (f5). do repeat b5 = B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 B5_AGE$06 B5_AGE$07 B5_AGE$08 B5_AGE$09 B5_AGE$10 B5_AGE$11 B5_AGE$12 B5_AGE$13 B5_AGE$14 B5_AGE$15 B5_AGE$16 B5_AGE$17 B5_AGE$18 B5_AGE$19 B5_AGE$20 B5_AGE$21 B5_AGE$22 B5_AGE$23 B5_AGE$24 B5_AGE$25 B5_AGE$26 B5_AGE$27 B5_AGE$28 B5_AGE$29 B5_AGE$30/ b8 = B8#1 to B8#30/. if b8 eq 1 and range(b5,3,18) totalinschool = totalinschool + 1. end repeat. Art Kendall Social Research Consultants On 9/17/2010 2:55 PM, Deepa Lakshmi Shanadi wrote: ===================== 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
Art Kendall
Social Research Consultants |
|
In reply to this post by Deepa Lakshmi Shanadi
Deepa
Interesting question for which Art has already
supplied syntax. If you need to come back with more requests for help, it
would be useful if you could let me have a list of the variables in your
file. Can you do...?
display labels .
. . . and send me the output?
There are some tutorials on COUNT in Block 3 on my
site, but your request is more complex. If your data set is not overly
large I can probably offer more specific advice and assistance
off-list.
|
|
In reply to this post by Deepa Lakshmi Shanadi
At 02:55 PM 9/17/2010, Deepa Lakshmi Shanadi wrote:
I need to count the total number of cases in 30 columns of children that are aged 3 to 18 years old. (The EXECUTE, of course, is neither necessary nor helpful.) But now I want to count the total number of cases in the 30 columns of those children that are aged 3 to 18 AND are going to school. The other variable is currently going to school= B8- 1= yes, 2=no, SO if the variable for going to School is B8#1, B8#2, etc to B8#30, where do I put the DO If statement in? This is an instance of multiple logical records (one for each child for each age) in a single physical record. In database parlance, the file is not 'normalized'. My preference (hi, David Marso) is to 'unroll' the file to 'long', or normalized, form. The syntax after that is much more direct, including easier to understand and debug; and other uses of the file will likely be easier in the 'long' form, as well. To 'unroll', something like this (untested): VARSTOCASES /MAKE Age FROM B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 B5_AGE$06 B5_AGE$07 B5_AGE$08 B5_AGE$09 B5_AGE$10 B5_AGE$11 B5_AGE$12 B5_AGE$13 B5_AGE$14 B5_AGE$15 B5_AGE$16 B5_AGE$17 B5_AGE$18 B5_AGE$19 B5_AGE$20 B5_AGE$21 B5_AGE$22 B5_AGE$23 B5_AGE$24 B5_AGE$25 B5_AGE$26 B5_AGE$27 B5_AGE$28 B5_AGE$29 B5_AGE$30 /MAKE In_School FROM B8#1 B8#2 B8#3 B8#4 B8#5 B5_AGE$06 B5_AGE$07 B5_AGE$08 B5_AGE$09 B5_AGE$10 B5_AGE$11 B5_AGE$12 B5_AGE$13 B5_AGE$14 B5_AGE$15 B5_AGE$16 B5_AGE$17 B5_AGE$18 B5_AGE$19 B5_AGE$20 B5_AGE$21 B5_AGE$22 B5_AGE$23 B5_AGE$24 B5_AGE$25 B5_AGE$26 B5_AGE$27 B5_AGE$28 B5_AGE$29 B5_AGE$30 /INDEX = AgeRec#(30) /KEEP = Child_ID /NULL = DROP. Here's a complete example (tested), with five different ages. If you start with |-----------------------------|---------------------------| |Output Created |20-SEP-2010 19:59:24 | |-----------------------------|---------------------------| [ShortForm] Chil d_ID B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 B8#1 B8#2 B8#3 B8#4 B8#5 001 20 11 15 8 2 1 0 1 1 1 002 10 16 14 2 6 1 0 0 1 1 003 18 11 16 3 15 1 1 0 0 1 Number of cases read: 3 Number of cases listed: 3 then, like this (tested): VARSTOCASES /MAKE Age FROM B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 /MAKE In_School FROM B8#1 B8#2 B8#3 B8#4 B8#5 /INDEX = AgeRec#(5) /KEEP = Child_ID /NULL = DROP. Variables to Cases |-----------------------------|---------------------------| |Output Created |20-SEP-2010 19:59:25 | |-----------------------------|---------------------------| [LongForm] Generated Variables |---------|------| |Name |Label | |---------|------| |AgeRec# |<none>| |Age |<none>| |In_School|<none>| |---------|------| Processing Statistics |-------------|--| |Variables In |11| |Variables Out|4 | |-------------|--| COMPUTE Age3to18_InSch = RANGE(Age,3,18) AND In_School. LIST. List |-----------------------------|---------------------------| |Output Created |20-SEP-2010 19:59:26 | |-----------------------------|---------------------------| [LongForm] Child_ID AgeRec# Age In_School Age3to18_InSch 001 1 20 1 .00 001 2 11 0 .00 001 3 15 1 1.00 001 4 8 1 1.00 001 5 2 1 .00 002 1 10 1 1.00 002 2 16 0 .00 002 3 14 0 .00 002 4 2 1 .00 002 5 6 1 1.00 003 1 18 1 1.00 003 2 11 1 1.00 003 3 16 0 .00 003 4 3 0 .00 003 5 15 1 1.00 Number of cases read: 15 Number of cases listed: 15 DATASET DECLARE Counts. AGGREGATE OUTFILE =Counts /BREAK =Child_ID /Total 'Ages 3-18 and in school' = SUM(Age3to18_InSch). ============================= APPENDIX: Test data, and code ============================= * ................. Test data ..................... . SET RNG = MT /* 'Mersenne twister' random number generator */ . SET MTINDEX = 6789 /* (Just thrown in) */ . INPUT PROGRAM. . NUMERIC Child_ID (N3). . NUMERIC B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 (F2) B8#1 B8#2 B8#3 B8#4 B8#5 (F1). LOOP Child_ID = 1 TO 3. . DO REPEAT Age = B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 /In_School = B8#1 B8#2 B8#3 B8#4 B8#5. . COMPUTE Age =TRUNC(RV.UNIFORM(1,21)). . COMPUTE In_School=RV.BERNOULLI(0.7). . END REPEAT. . END CASE. END LOOP. END FILE. END INPUT PROGRAM. DATASET NAME ShortForm WINDOW=FRONT. LIST. DATASET ACTIVATE ShortForm WINDOW=FRONT /* helpful when debugging */. DATASET COPY LongForm. DATASET ACTIVATE LongForm WINDOW=FRONT. * ................. Working code ..................... . * ................................................................. . VARSTOCASES /MAKE Age FROM B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 /MAKE In_School FROM B8#1 B8#2 B8#3 B8#4 B8#5 /INDEX = AgeRec#(5) /KEEP = Child_ID /NULL = DROP. LIST. COMPUTE Age3to18_InSch = RANGE(Age,3,18) AND In_School. LIST. DATASET DECLARE Counts. AGGREGATE OUTFILE =Counts /BREAK =Child_ID /Total 'Ages 3-18 and in school' = SUM(Age3to18_InSch). DATASET ACTIVATE Counts WINDOW=FRONT. FORMATS Total (F3). 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 |
|
In reply to this post by Art Kendall
the TO convention on a list of variables (varlist in the syntax
documentation) allows you to specify a set of variables that are in
columns that are next to each other in the data file. You can see
this in the variable view or across the top of the data view.
if these 7 variables are next to each other (contiguous) in the data file B5_AGE$01 B5_AGE$02 B5_AGE$03 B5_AGE$04 B5_AGE$05 B5_AGE$06 B5_AGE$07 you do not have to type the whole list you can just type B5_AGE$01 TO B5_AGE$07 In the snippet of syntax you just posted for the do repeat B5 is defined by typing out the whole variable list as if the variables were scattered across the data file. B8 is defined as if for that list of variables the variables were next to each other. inschool creates a new set of 30 variables that are next to each other. the first error message tells you that the variable totalinschool already exists, most likely from an earlier use of the same syntax. You have an extra period which tells SPSS you have finished the DO REPEAT command. Since you finished a command SPSS is looking for a new command and B8 is not a legitimate command B5_AGE$28 B5_AGE$29 B5_AGE$30/. Did the syntax with simulated data work the way you wanted? I suggest you work with the simulated data until you have logic that does what you want. Art Kendall Social Research Consultants On 9/21/2010 1:45 PM, Deepa Lakshmi Shanadi wrote: ===================== 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
Art Kendall
Social Research Consultants |
| Free forum by Nabble | Edit this page |
