Moving Variables into Groups

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

Moving Variables into Groups

Henry Park
This post was updated on .
I have several datasets that need to have their variables rearranged according to a common number in each variable name.  For example let's say I have variables that look like:
type.1234.a type.2323.b response.1234.a response.2323.b correct.5564.c response.5564.a correct.1234.d

Is there a way I can write syntax or use python to move the variables with the same number into a group?  So instead of all of the 1234 variables being everywhere they are at least clustered together?  Also is there a way to tell it how to organize said group by the beginning phrases in each variable?  

I wanted to add a few more details:
Here is a sample from the codebook
Type.3905.c
Response.3905.c
Correct.3905.c
Rescored.3905.c
Type.3906.a
Response.3906.a
Correct.3906.a
Time_sum_itempage.3758.a
Time_sum_itempage.3758.b
Time_sum_itempage.3905.a
Time_sum_itempage.3905.b
Time_sum_itempage.3905.c
Time_sum_itempage.3906.a
I_3747d_PointsPossible
I_3747e_PointsPossible
I_3748aa_PointsPossible
I_3748ab_PointsPossible
I_3748b_PointsPossible
I_3748da_PointsPossible
I_3748db_PointsPossible
I_3749a_PointsPossible

I would need to get them organized by number so as it would look something like:
a1 b1 c1 a2 b2 b3 c3

As of now the groups are split up and spread throughout the database but we need them organized to do the analysis.  Thanks.


Reply | Threaded
Open this post in threaded view
|

Re: Moving Variables into Groups

Albert-Jan Roskam
Have you tried using SORT VARIABLES? If there are always four digits, this will work. If not, it'll look better but it won't be perfect because the sorting is done alphanumerically.
 
Regards,
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a
fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

From: Henry Park <[hidden email]>
To: [hidden email]
Sent: Wednesday, July 18, 2012 7:04 PM
Subject: [SPSSX-L] Moving Variables into Groups

I have several datasets that need to have their variables rearranged
according to a common number in each variable name.  For example let's say I
have variables that look like:
type.1234.a type.2323.b response.1234.a response.2323.b correct.5564.c
response.5564.a correct.1234.d

Is there a way I can write syntax or use python to move the variables with
the same number into a group?  So instead of all of the 1234 variables being
everywhere they are at least clustered together?  Also is there a way to
tell it how to organize said group by the beginning phrases in each
variable?


--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Moving-Variables-into-Groups-tp5714291.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


Reply | Threaded
Open this post in threaded view
|

Re: Moving Variables into Groups

Jon K Peck
In reply to this post by Henry Park
This is easily done with a little Python code.  Here is an example that sorts the variable names by an expected numerical second part.  The key is to build the list for the match files /keep subcommand.  The sorting logic can, of course, be changed.  Note the indentations after def cmp.  If the email messes these up, email me offline.

data list free /type.1234.a type.2323.b response.1234.a response.2323.b correct.5564.c
response.5564.a correct.1234.d.
begin data.
1 1 1 1 1 1 1.
end data.
dataset name input.

begin program.
import spss, spssaux
allvars = spssaux.VariableDict().variables
def cmp(x,y):
    xk = x.split(".")[1]
    yk = y.split(".")[1]
    if xk < yk:
        return -1
    elif xk > yk:
        return 1
    else:
      return 0

reordered = sorted(allvars, cmp = cmp)
spss.Submit("match files /file=* /keep " + " ".join(reordered))
end program.

Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
new phone: 720-342-5621




From:        Henry Park <[hidden email]>
To:        [hidden email]
Date:        07/18/2012 11:26 AM
Subject:        [SPSSX-L] Moving Variables into Groups
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




I have several datasets that need to have their variables rearranged
according to a common number in each variable name.  For example let's say I
have variables that look like:
type.1234.a type.2323.b response.1234.a response.2323.b correct.5564.c
response.5564.a correct.1234.d

Is there a way I can write syntax or use python to move the variables with
the same number into a group?  So instead of all of the 1234 variables being
everywhere they are at least clustered together?  Also is there a way to
tell it how to organize said group by the beginning phrases in each
variable?


--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Moving-Variables-into-Groups-tp5714291.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


Reply | Threaded
Open this post in threaded view
|

Re: Moving Variables into Groups

Henry Park
Does this section represent the number of variables in the data list?
begin data.
1 1 1 1 1 1 1.
end data.

so if I have say 278 variables this would be a bit challenging.  I can copy and paste to the datalist but I don't know if I can accurately count out 278 1's.
Reply | Threaded
Open this post in threaded view
|

Re: Moving Variables into Groups

Ruben Geert van den Berg
Dear Henry,

The data list part merely creates some fake data needed in order to demonstrate the actual solution for your problem. You're supposed to run only the syntax that comes after "end data." while you have your own data file opened. I think the syntax below should work for you. Mind you that:

-The sorting is applied only to variables whose names contain (exactly) two dots. All other variables will be included in the end result (keeping their original order) after the block of sorted variables.
-The syntax assumes that the number of digits in the variable names are constant (in your case: always 4 digits). This is because the sorting is alphabetical so "3" would come after "22".

Of course, these 2 features can be modified if desired.

HTH,

Ruben

begin program.
vars=[]
for i in spssaux.GetVariableNamesList():
    if i.count(".")==2:
        vars.append(i)
def createkey(s1):
    key=s1.split(".")[2]+s1.split(".")[1]
    return key
spss.Submit("Match files file = */keep "+" ".join(sorted(vars,key=createkey))+" all.\nexecute.")
end program.


> Date: Wed, 18 Jul 2012 12:55:23 -0700

> From: [hidden email]
> Subject: Re: Moving Variables into Groups
> To: [hidden email]
>
> Does this section represent the number of variables in the data list?
> begin data.
> 1 1 1 1 1 1 1.
> end data.
>
> so if I have say 278 variables this would be a bit challenging. I can copy
> and paste to the datalist but I don't know if I can accurately count out 278
> 1's.
>
> --
> View this message in context: http://spssx-discussion.1045642.n5.nabble.com/Moving-Variables-into-Groups-tp5714291p5714297.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