Quantcast

Sort Variables

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Sort Variables

emma78
Hi,
we need to create sorted variables lists.

Our data looks like this:

ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_27
v_28
v_29
v_25_1
v_25_2
v_25_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3
v_26_1
v_26_2
v_26_3


It should like:
ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_25_1
v_25_2
v_25_3
v_26_1
v_26_2
v_26_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3

v_27
v_28
v_29




In this little example only the three variables with v_26 aren't in the right order and the whole block with the _variables are at the end. Of course I can do that manually but if there are thousands of them it will be difficult.


Is there a possibility to sort them in the right order? I can also do that with the menu and sort by name but then the whole block is still at the end.


Many thanks for your suggestions!
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sort Variables

Bruce Weaver
Administrator
Hello Emma.  I am confused by the comment you made after showing the desired variable order.

You said, "only the three variables with v_26 aren't in the right order and the whole block with the _variables are at the end".

Did you mean that THIS is the desired order?

ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_27
v_28
v_29
v_25_1
v_25_2
v_25_3
v_26_1
v_26_2
v_26_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3

If that *is* what you meant, try this:

SORT VARIABLES BY NAME (A).
ADD FILES FILE=* /KEEP=ID ALL.
EXECUTE.

HTH.


emma78 wrote
Hi,
we need to create sorted variables lists.

Our data looks like this:

ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_27
v_28
v_29
v_25_1
v_25_2
v_25_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3
v_26_1
v_26_2
v_26_3


It should like:
ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_25_1
v_25_2
v_25_3
v_26_1
v_26_2
v_26_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3

v_27
v_28
v_29




In this little example only the three variables with v_26 aren't in the right order and the whole block with the _variables are at the end. Of course I can do that manually but if there are thousands of them it will be difficult.


Is there a possibility to sort them in the right order? I can also do that with the menu and sort by name but then the whole block is still at the end.


Many thanks for your suggestions!
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

NOTE: My Hotmail account is not monitored regularly.
To send me an e-mail, please use the address shown above.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sort Variables

emma78
In reply to this post by emma78
Hi,
I made a mistake, sorry for the confusion:

ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_30
v_31
v_32
v_25_1
v_25_2
v_25_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3
v_26_1
v_26_2
v_26_3

and it shoud like
ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_25_1
v_25_2
v_25_3
v_26_1
v_26_2
v_26_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3
v_30
v_31
v_32
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sort Variables

Jon Peck
In reply to this post by emma78
Assuming
- you want to sort the names according to the numeric value of the number strings.  Sorted as numbers, you would get 1,2,3,...25,26...
Sorted as strings you would get
1,2,25,26,...,3, 4,...

- these names are in an existing variable dictionary

- all the names except ID should be alphabetized.  ID should remain first

Try this code.

begin program.
import spss, spssaux

varlist = spssaux.VariableDict().variables
varlist.pop(varlist.index('ID'))

def f(key):
    key = key.split("_")
    for k, item in enumerate(key):
        try:
            item = int(item)
        except:
            pass
        key[k] = item
    return key

s = sorted(varlist, key=f)

spss.Submit(r"""ADD FILES FILE=*
/KEEP=ID %s.""" % " ".join(s))
end program.





On Wed, Apr 20, 2016 at 9:17 AM, emma78 <[hidden email]> wrote:
Hi,
we need to create sorted variables lists.

Our data looks like this:

ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_27
v_28
v_29
v_25_1
v_25_2
v_25_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3
v_26_1
v_26_2
v_26_3


It should like:
ID
c_0001
c_0002
c_0003
v_1
v_2
v_3
v_4
v_5
v_6
v_25_1
v_25_2
v_25_3
v_26_1
v_26_2
v_26_3
v_27_1
v_27_2
v_27_3
v_28_1
v_28_2
v_28_3
v_29_1
v_29_2
v_29_3

v_27
v_28
v_29




In this little example only the three variables with v_26 aren't in the
right order and the whole block with the _variables are at the end. Of
course I can do that manually but if there are thousands of them it will be
difficult.


Is there a possibility to sort them in the right order? I can also do that
with the menu and sort by name but then the whole block is still at the end.


Many thanks for your suggestions!




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



--
Jon K Peck
[hidden email]

===================== 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
|  
Report Content as Inappropriate

Re: Sort Variables

emma78
Hi,
is it possible to sort this variable list
lfdn
v_20_1
v_20_2
v_20_3
v_20_4
v_20_5
v_20_6
v_20_7
v_20_8
v_20_9
v_20_10
v_20_11
v_20_12
v_20_13
v_20_14
v_20_15
v_20_16
v_21_1
v_21_2
v_21_3
v_21_4
v_21_5
v_21_6
v_21_7
v_21_8
v_21_9
v_21_10
v_21_11
v_21_12
v_21_13
v_21_14
v_21_15
v_21_16

that it looks like
lfdn
v_20_1
v_21_1
v_20_2
v_21_2
v_20_3
v_21_3
v_20_4
v_21_4
v_20_5
v_21_5
v_20_6
v_21_6
v_20_7
v_21_7
v_20_8
v_21_8
v_20_9
v_21_9
v_20_10
v_21_10
v_20_11
v_21_11
v_20_12
v_21_12
v_20_13
v_21_13
v_20_14
v_21_14
v_20_15
v_21_15
v_20_16
v_21_16

I already tried to adapt the Python script but it doesn't work :-(


begin program.
import spss, spssaux

varlist = spssaux.VariableDict().variables
varlist.pop(varlist.index('lfdn'))

def f(key):
    key = key.split("_")
    for k, item in enumerate(key):
        try:
            item = int(item)
        except:
            pass
        key[k] = item
    return key

s = sorted(varlist, key=f)

spss.Submit(r"""ADD FILES FILE=*
/KEEP=lfdn  %s.""" % " ".join(s))
end program.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sort Variables

John F Hall
Emma

In haste and untested, but supply your own <full file pathname>, then
something like:

save out '<full file pathname>'
        /keep lfdn
v_20_1
v_21_1
v_20_2
v_21_2
v_20_3
v_21_3
v_20_4
v_21_4
v_20_5
v_21_5
v_20_6
v_21_6
v_20_7
v_21_7
v_20_8
v_21_8
v_20_9
v_21_9
v_20_10
v_21_10
v_20_11
v_21_11
v_20_12
v_21_12
v_20_13
v_21_13
v_20_14
v_21_14
v_20_15
v_21_15
v_20_16
v_21_16.


John F Hall (Mr)
[Retired academic survey researcher]

Email:   [hidden email]  
Website: www.surveyresearch.weebly.com
SPSS start page:
www.surveyresearch.weebly.com/1-survey-analysis-workshop




-----Original Message-----
From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf
Of emma78
Sent: 07 February 2017 16:26
To: [hidden email]
Subject: Re: Sort Variables

Hi,
is it possible to sort this variable list lfdn
v_20_1
v_20_2
v_20_3
v_20_4
v_20_5
v_20_6
v_20_7
v_20_8
v_20_9
v_20_10
v_20_11
v_20_12
v_20_13
v_20_14
v_20_15
v_20_16
v_21_1
v_21_2
v_21_3
v_21_4
v_21_5
v_21_6
v_21_7
v_21_8
v_21_9
v_21_10
v_21_11
v_21_12
v_21_13
v_21_14
v_21_15
v_21_16

that it looks like

I already tried to adapt the Python script but it doesn't work :-(


begin program.
import spss, spssaux

varlist = spssaux.VariableDict().variables
varlist.pop(varlist.index('lfdn'))

def f(key):
    key = key.split("_")
    for k, item in enumerate(key):
        try:
            item = int(item)
        except:
            pass
        key[k] = item
    return key

s = sorted(varlist, key=f)

spss.Submit(r"""ADD FILES FILE=*
/KEEP=lfdn  %s.""" % " ".join(s))
end program.



--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Sort-Variables-tp5731970
p5733780.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
|  
Report Content as Inappropriate

Re: Sort Variables

Jon Peck
In reply to this post by emma78
Try this as your key function.

def f(key):
    key = key.split("_")
    return (int(key[2]), int(key[1]))

On Tue, Feb 7, 2017 at 8:26 AM, emma78 <[hidden email]> wrote:
Hi,
is it possible to sort this variable list
lfdn
v_20_1
v_20_2
v_20_3
v_20_4
v_20_5
v_20_6
v_20_7
v_20_8
v_20_9
v_20_10
v_20_11
v_20_12
v_20_13
v_20_14
v_20_15
v_20_16
v_21_1
v_21_2
v_21_3
v_21_4
v_21_5
v_21_6
v_21_7
v_21_8
v_21_9
v_21_10
v_21_11
v_21_12
v_21_13
v_21_14
v_21_15
v_21_16

that it looks like
lfdn
v_20_1
v_21_1
v_20_2
v_21_2
v_20_3
v_21_3
v_20_4
v_21_4
v_20_5
v_21_5
v_20_6
v_21_6
v_20_7
v_21_7
v_20_8
v_21_8
v_20_9
v_21_9
v_20_10
v_21_10
v_20_11
v_21_11
v_20_12
v_21_12
v_20_13
v_21_13
v_20_14
v_21_14
v_20_15
v_21_15
v_20_16
v_21_16

I already tried to adapt the Python script but it doesn't work :-(


begin program.
import spss, spssaux

varlist = spssaux.VariableDict().variables
varlist.pop(varlist.index('lfdn'))

def f(key):
    key = key.split("_")
    for k, item in enumerate(key):
        try:
            item = int(item)
        except:
            pass
        key[k] = item
    return key

s = sorted(varlist, key=f)

spss.Submit(r"""ADD FILES FILE=*
/KEEP=lfdn  %s.""" % " ".join(s))
end program.



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



--
Jon K Peck
[hidden email]

===================== 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
|  
Report Content as Inappropriate

Re: Sort Variables

emma78
That is working thank you!
Is it posible to adajust it, just in case we have some variables with only one '_' in the name?
That these two stays before the v_20_XXX? In the moment the syntax isn't working, if I keep this variables in the dataset, thank you!
So
lfdn
v_2
v_3
v_20_1
v_20_2
v_20_3
v_20_4
v_20_5
v_20_6
v_20_7
v_20_8
v_20_9
v_20_10
v_20_11
v_20_12
v_20_13
v_20_14
v_20_15
v_20_16
v_21_1
v_21_2
v_21_3
v_21_4
v_21_5
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Sort Variables

Jon Peck
This key function will do it.

def f(key):
    key = key.split("_")
    if len(key) == 3:
        return (int(key[2]), int(key[1]))
    else:
        return (0, int(key[1]))

On Fri, Feb 10, 2017 at 3:15 AM, emma78 <[hidden email]> wrote:
That is working thank you!
Is it posible to adajust it, just in case we have some variables with only
one '_' in the name?
That these two stays before the v_20_XXX? In the moment the syntax isn't
working, if I keep this variables in the dataset, thank you!
So
lfdn
v_2
v_3
v_20_1
v_20_2
v_20_3
v_20_4
v_20_5
v_20_6
v_20_7
v_20_8
v_20_9
v_20_10
v_20_11
v_20_12
v_20_13
v_20_14
v_20_15
v_20_16
v_21_1
v_21_2
v_21_3
v_21_4
v_21_5



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



--
Jon K Peck
[hidden email]

===================== 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
Loading...