# Sort Variables

14 messages
Open this post in threaded view
|

## Sort Variables

 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!
Open this post in threaded view
|

## Re: Sort Variables

 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.
Open this post in threaded view
|

## Re: Sort Variables

 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
Open this post in threaded view
|

## Re: Sort Variables

 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 get1,2,25,26,...,3, 4,...- these names are in an existing variable dictionary- all the names except ID should be alphabetized.  ID should remain firstTry this code.begin program.import spss, spssauxvarlist = spssaux.VariableDict().variablesvarlist.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 keys = 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 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
Open this post in threaded view
|

## 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 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.
Open this post in threaded view
|

## Re: Sort Variables

 Emma In haste and untested, but supply your own , then something like: save out ''         /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-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 ===================== 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
Open this post in threaded view
|

## Re: Sort Variables

 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 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
Open this post in threaded view
|

## Re: Sort Variables

 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
Open this post in threaded view
|

## Re: Sort Variables

 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 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
Open this post in threaded view
|

## Re: Sort Variables

 Hi, another question regarding this sorting problem lfdn v_1 v_55 v_40_1 v_40_2 v_40_3 v_40_4 v_41_1 v_41_2 v_41_3 v_41_4 v_42_1 v_42_2 v_42_3 v_42_4 v_43_1 should look like lfdn v_1 v_40_1 v_41_1 v_42_1 v_43_1 v_40_2 v_41_2 v_42_2 v_40_3 v_41_3 v_42_3 v_40_4 v_41_4 v_42_4 v_55 I already got this script begin program. import spss, spssaux varlist = spssaux.VariableDict().variables varlist.pop(varlist.index('lfdn')) def f(key):     key = key.split("_")     if len(key) == 3:         return (int(key[2]), int(key[1]))     else:         return (0, int(key[1]))     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 duration  %s.""" % " ".join(s)) end program. but the v_55 stays the same and doesn`t appear after the v_42_4. Any suggestion on this? Another problem occurs in a dataset with a lot of variables, I got this message Traceback (most recent call last):   File "", line 18, in   File "", line 10, in f IndexError: list index out of range Anybody knows what this means?
Open this post in threaded view
|

## Re: Sort Variables

 Emma78, I also recommend you to consider one of my old macros called !INTLACE, found on http://www.spsstools.net/en/KO-spssmacros/ in collection "Job tools". It does not sorts, but it re-arranges (interlaces) elements of blocks of names. It is handy to change the order of variables in a list when you have some blocks of names, say, X1 X2  Y1 Y2  Z1 Z2; the macro will re-arrange the order into: X1 Y1 Z1 X2 Y2 Z 2. Use tilda to separate your blocks. Blocks do not need be equal length. *Mode 1. Print out result. !intlace (PRINT)  v_1 ~  v_55 ~  v_40_1  v_40_2  v_40_3  v_40_4 ~  v_41_1  v_41_2  v_41_3  v_41_4 ~  v_42_1  v_42_2  v_42_3  v_42_4 ~  v_43_1. Prints out result as this list:  v_1 v_55 v_40_1 v_41_1 v_42_1 v_43_1 v_40_2 v_41_2 v_42_2 v_40_3 v_41_3 v_42_3 v_40_4 v_41_4 v_42_4 *Mode2. Insert result in a command. match files /file= * /keep= !intlace  v_1 ~  v_55 ~  v_40_1  v_40_2  v_40_3  v_40_4 ~  v_41_1  v_41_2  v_41_3  v_41_4 ~  v_42_1  v_42_2  v_42_3  v_42_4 ~  v_43_1. exec. Produces ans runs the following syntax: match files /file= *   /keep= v_1 v_55 v_40_1 v_41_1 v_42_1 v_43_1 v_40_2 v_41_2 v_42_2 v_40_3          v_41_3 v_42_3 v_40_4 v_41_4 v_42_4. exec. ===================== 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
Open this post in threaded view
|

## Re: Sort Variables

 Just change the line after "else" to read    else:        return (int(key[1]), 0)On Fri, Mar 3, 2017 at 4:20 AM, Kirill Orlov wrote: Emma78, I also recommend you to consider one of my old macros called !INTLACE, found on http://www.spsstools.net/en/KO-spssmacros/ in collection "Job tools". It does not sorts, but it re-arranges (interlaces) elements of blocks of names. It is handy to change the order of variables in a list when you have some blocks of names, say, X1 X2  Y1 Y2  Z1 Z2; the macro will re-arrange the order into: X1 Y1 Z1 X2 Y2 Z 2. Use tilda to separate your blocks. Blocks do not need be equal length. *Mode 1. Print out result. !intlace (PRINT)  v_1 ~  v_55 ~  v_40_1  v_40_2  v_40_3  v_40_4 ~  v_41_1  v_41_2  v_41_3  v_41_4 ~  v_42_1  v_42_2  v_42_3  v_42_4 ~  v_43_1. Prints out result as this list:  v_1 v_55 v_40_1 v_41_1 v_42_1 v_43_1 v_40_2 v_41_2 v_42_2 v_40_3 v_41_3 v_42_3 v_40_4 v_41_4 v_42_4 *Mode2. Insert result in a command. match files /file= * /keep= !intlace  v_1 ~  v_55 ~  v_40_1  v_40_2  v_40_3  v_40_4 ~  v_41_1  v_41_2  v_41_3  v_41_4 ~  v_42_1  v_42_2  v_42_3  v_42_4 ~  v_43_1. exec. Produces ans runs the following syntax: match files /file= *   /keep= v_1 v_55 v_40_1 v_41_1 v_42_1 v_43_1 v_40_2 v_41_2 v_42_2 v_40_3          v_41_3 v_42_3 v_40_4 v_41_4 v_42_4. exec. ===================== 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