|
OK, here's the kludgy way to do it:
STRING AgeGroup (A4). IF (AGEATAPP<16) AgeGroup='1'. VARIABLE LABELS AgeGroup 'Age Group'. EXECUTE. IF (AGEATAPP>=27) AgeGroup='4'. VARIABLE LABELS AgeGroup 'Age Group'. EXECUTE. IF (AGEATAPP>=14.00 AND AGEATAPP<22) AgeGroup='2'. VARIABLE LABELS AgeGroup 'Age Group'. EXECUTE. IF (AGEATAPP>=22.00 AND AGEATAPP<27) AgeGroup='3'. VARIABLE LABELS AgeGroup 'Age Group'. EXECUTE. However, I'd like to do the same thing more efficiently, with fewer lines of code. Will this work better to achieve the same results? STRING AgeGroup (A4). VARIABLE LABELS AgeGroup 'Age Group'. COMPUTE AgeGroup = '4'. DO IF (AGEATAPP<27) Compute AgeGroup='3'. ELSE IF (AGEATAPP<22) Compute AgeGroup='2'. ELSE IF (AGEATAPP<16) Compute AgeGroup='1'. END IF . Is there an even better way? Thanks, Bob Schacht Robert M. Schacht, Ph.D. <[hidden email]> Pacific Basin Rehabilitation Research & Training Center 1268 Young Street, Suite #204 Research Center, University of Hawaii Honolulu, HI 96814 ===================== 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 |
|
If you want Agegroup to be numeric NUMERIC agegroup (f1) and drop the single quotes. Open a new instance of SPSS. Copy the syntax below into a syntax window. Run it. Is this what you want? Assumed 14 was a typo. DATA LIST list/id (f2) ageatapp(f2). BEGIN DATA 1 15 2 16 3 21 4 22 5 26 6 27 7 28 8 xx end data. STRING AgeGroup (A4). RECODE ageatapp (lo thru 15.99 = '1') (16 thru 21.99 = '2') (22 thru 26.99 = '3') (27 thru hi = '4') (else = '5') into agegroup. VARIABLE LABELS AgeGroup 'Age Group'. missing values agegroup ('5'). list. Art Kendall Social Research Consultants Bob Schacht wrote: OK, here's the kludgy way to do it:
Art Kendall
Social Research Consultants |
|
In reply to this post by Bob Schacht-3
Bob
Neither will work as is. In your first code snippet, you have an overlap between age groups 1 nad 2. A 15 year old gets assigned to group 1, then further down is re-assigned to group 2. In your second snippet,remember that once a condition is met, subsequent conditions are not tested therefore the first condition will assign anyone under 27 to aage group 3, and the subsequent elseif commands will not be executed. If you invert the order (start with ageatapp <16), it would work. Garry Gelade Business Analytic Ltd. -----Original Message----- From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Bob Schacht Sent: 14 February 2009 00:45 To: [hidden email] Subject: Defining age groups OK, here's the kludgy way to do it: STRING AgeGroup (A4). IF (AGEATAPP<16) AgeGroup='1'. VARIABLE LABELS AgeGroup 'Age Group'. EXECUTE. IF (AGEATAPP>=27) AgeGroup='4'. VARIABLE LABELS AgeGroup 'Age Group'. EXECUTE. IF (AGEATAPP>=14.00 AND AGEATAPP<22) AgeGroup='2'. VARIABLE LABELS AgeGroup 'Age Group'. EXECUTE. IF (AGEATAPP>=22.00 AND AGEATAPP<27) AgeGroup='3'. VARIABLE LABELS AgeGroup 'Age Group'. EXECUTE. However, I'd like to do the same thing more efficiently, with fewer lines of code. Will this work better to achieve the same results? STRING AgeGroup (A4). VARIABLE LABELS AgeGroup 'Age Group'. COMPUTE AgeGroup = '4'. DO IF (AGEATAPP<27) Compute AgeGroup='3'. ELSE IF (AGEATAPP<22) Compute AgeGroup='2'. ELSE IF (AGEATAPP<16) Compute AgeGroup='1'. END IF . Is there an even better way? Thanks, Bob Schacht Robert M. Schacht, Ph.D. <[hidden email]> Pacific Basin Rehabilitation Research & Training Center 1268 Young Street, Suite #204 Research Center, University of Hawaii Honolulu, HI 96814 ===================== 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 __________ NOD32 3852 (20090213) Information __________ This message was checked by NOD32 antivirus system. http://www.eset.com ===================== 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 |
