Hi all,
I'm computing a new variable x2 with 9 categories from x1 which is 122 categories, my syntax looks like this: compute x2=$sysmis. (if x1=2 or x1=6 or x1=65 or x1=68 or x1=79) x2=1. (if x1 = 3 or x1 = 7 or x1=10 or x1 = 12) x2 = 2. ........etc This works just fine. But does more efficient syntax exist for something like this? Some of these new x2 categories include 40 or so categories of x1 making for a high probability of screwing up. It's a new year and I should learn some new syntax. Thanks much. Carol |
When I get that many categories (and the order is irregular) I don't bother with IF or whatever, but actually make a separate look up table to define the second category. E.g. I would have a table that maps x1 to x2;
X1 X2 2 1 6 1 65 1 68 1 79 1 3 2 7 2 10 2 12 2 etc.... Then having this as a separate SPSS data file, the code is simply: SORT CASES BY x1. MATCH FILES FILE = * /TABLE = 'LookUpTable' /BY x1. |
Makes sense for big category lists, but
the any function would also simplify the posted code a lot.
Jon Peck (no "h") aka Kim Senior Software Engineer, IBM [hidden email] phone: 720-342-5621 From: Andy W <[hidden email]> To: [hidden email] Date: 01/02/2015 10:25 AM Subject: Re: [SPSSX-L] More efficient code? Sent by: "SPSSX(r) Discussion" <[hidden email]> When I get that many categories (and the order is irregular) I don't bother with IF or whatever, but actually make a separate look up table to define the second category. E.g. I would have a table that maps x1 to x2; X1 X2 2 1 6 1 65 1 68 1 79 1 3 2 7 2 10 2 12 2 etc.... Then having this as a separate SPSS data file, the code is simply: SORT CASES BY x1. MATCH FILES FILE = * /TABLE = 'LookUpTable' /BY x1. ----- Andy W [hidden email] http://andrewpwheeler.wordpress.com/ -- View this message in context: http://spssx-discussion.1045642.n5.nabble.com/More-efficient-code-tp5728321p5728322.html Sent from the SPSSX Discussion mailing list archive at Nabble.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 ===================== 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 |
Administrator
|
In reply to this post by parisec
You could go with a LOOKUP table as Andy suggested.
Or see ANY function and RECODE. COMPUTE x2=SUM(ANY(x1, 2, 6, 65, 68, 79)*1, ANY(x1, 3, 7, 10, 12 )*2) ......etc. or RECODE x1 (2 ,6 ,65 ,68 ,79 =1)(3,7,10,12=2) INTO x2 . Of these 2 the RECODE is surely more efficient. ----
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me. --- "Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis." Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?" |
Free forum by Nabble | Edit this page |