More efficient code?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

More efficient code?

parisec
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

Reply | Threaded
Open this post in threaded view
|

Re: More efficient code?

Andy W
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
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: More efficient code?

Jon K Peck
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
Reply | Threaded
Open this post in threaded view
|

Re: More efficient code?

David Marso
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.

----

parisec wrote
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
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?"
Reply | Threaded
Open this post in threaded view
|

Re: More efficient code?

parisec
In reply to this post by parisec
thanks all!  


Now i have three methods that are way better than what i have been doing.

Great way to start the year.

Carol