Macro for reordering variables

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

Macro for reordering variables

Ruben Geert van den Berg
Dear all,
 
I'm trying to write a macro that should reorder the variables in my file. Right now, I've Label.1 to Label.15 and then Leave.1 to Leave.15. I'd like the order to be Label.1 Leave.1 Label.2 Leave.2 (...) Label.15 Leave.15. Admittedly, I could write this by hand since it's only 30 variables but next time it could be 100 or 200 variables and I'd really like to learn some very basic macro language as well.
 
Thanks a lot in advance!

define !reorder ()
!do !t2=1 !to 15
!let !varlist=!concat('label.',!t2) !concat('leave.',!t2)
!doend
matc fil fil *
/kee variable_name !varlist
exe.
!end.
 
set mpr on.
!reorder.


 




Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|

Re: Macro for reordering variables

Bruce Weaver
Administrator
Ruben van den Berg wrote
Dear all,

 

I'm trying to write a macro that should reorder the variables in my file. Right now, I've Label.1 to Label.15 and then Leave.1 to Leave.15. I'd like the order to be Label.1 Leave.1 Label.2 Leave.2 (...) Label.15 Leave.15. Admittedly, I could write this by hand since it's only 30 variables but next time it could be 100 or 200 variables and I'd really like to learn some very basic macro language as well.

 

Thanks a lot in advance!


define !reorder ()

!do !t2=1 !to 15

!let !varlist=!concat('label.',!t2) !concat('leave.',!t2)

!doend

matc fil fil *

/kee variable_name !varlist

exe.

!end.

 

set mpr on.

!reorder.
You need to append !varlist to itself each time through the loop, and you need to leave spaces between the variable names in the list.  Try this:

define !reorder ()
!let !varlist=''
!do !t2=1 !to 15
!let !varlist=!concat(!varlist,' label.',!t2,' leave.',!t2)
!doend
match files file = *
/keep variable_name !varlist
exe.
!end.

--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).
Reply | Threaded
Open this post in threaded view
|

Re: Macro for reordering variables

Albert-Jan Roskam
In reply to this post by Ruben Geert van den Berg
Hi!
 
Here's a Python approach. Don't know how to do it in macro. Perhaps with N of cases = 1, then FLIP, SORT WRITE, etc.
 
* vector label. (50) / leave. (50). /* test variables.
 
begin program.
import spssaux
labels = spssaux.VariableDict(pattern = r"label")
leaves = spssaux.VariableDict(pattern = r"leave")
vlist = " ".join([label + " " + leave for label, leave in zip (labels, leaves)])
spss.Submit("add files / file = * / keep = %s all." % vlist)
end program.


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Wed, 11/25/09, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Macro for reordering variables
To: [hidden email]
Date: Wednesday, November 25, 2009, 10:25 AM

Dear all,
 
I'm trying to write a macro that should reorder the variables in my file. Right now, I've Label.1 to Label.15 and then Leave.1 to Leave.15. I'd like the order to be Label.1 Leave.1 Label.2 Leave.2 (...) Label.15 Leave.15. Admittedly, I could write this by hand since it's only 30 variables but next time it could be 100 or 200 variables and I'd really like to learn some very basic macro language as well.
 
Thanks a lot in advance!

define !reorder ()
!do !t2=1 !to 15
!let !varlist=!concat('label.',!t2) !concat('leave.',!t2)
!doend
matc fil fil *
/kee variable_name !varlist
exe.
!end.
 
set mpr on.
!reorder.


 




Express yourself instantly with MSN Messenger! MSN Messenger

Reply | Threaded
Open this post in threaded view
|

Re: Macro for reordering variables

Bob Walker-2
In reply to this post by Ruben Geert van den Berg

Hi Ruben,

 

Alternatively, using an example with five variables...

 

PRESERVE.

SET MEXPAND ON /MPRINT ON.

DATA LIST LIST /label.1 TO label.5 leave.1 TO leave.5.

BEGIN DATA

0 1 0 1 1 0 1 0 1 1

1 1 1 1 1 1 1 0 1 1

0 1 0 1 1 0 1 0 1 1

END DATA.

 

DEFINE RUBEN()

MATCH FILES FILE=* /KEEP !DO !Z = 1 !TO 5 !CONCAT("label.",!Z," leave.",!Z) !DOEND

!ENDDEFINE.

RUBEN.

RESTORE.

 

Here the looping is simply embedded within a /KEEP subcommand (which is OK)…

 

Regards,

 

Bob Walker

Surveys & Forecasts, LLC

www.safllc.com

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of Ruben van den Berg
Sent: Wednesday, November 25, 2009 4:26 AM
To: [hidden email]
Subject: Macro for reordering variables

 

Dear all,
 
I'm trying to write a macro that should reorder the variables in my file. Right now, I've Label.1 to Label.15 and then Leave.1 to Leave.15. I'd like the order to be Label.1 Leave.1 Label.2 Leave.2 (...) Label.15 Leave.15. Admittedly, I could write this by hand since it's only 30 variables but next time it could be 100 or 200 variables and I'd really like to learn some very basic macro language as well.
 
Thanks a lot in advance!

define !reorder ()
!do !t2=1 !to 15
!let !varlist=!concat('label.',!t2) !concat('leave.',!t2)
!doend
matc fil fil *
/kee variable_name !varlist
exe.
!end.
 
set mpr on.
!reorder.


 



Express yourself instantly with MSN Messenger! MSN Messenger

Reply | Threaded
Open this post in threaded view
|

Re: Macro for reordering variables

Ruben Geert van den Berg
In reply to this post by Bruce Weaver
Dear Bruce, Albert-Jan and Bob,
 
Thanks very much for the wonderful solutions! The two classic macro solutions work fine, the Python solution -unfortunately- made SPSS crash (it should work with V17.01 and Python 2.5, right?) but I'll try to run it again tomorrow at work because I'll have to start learning Python at some point anyway.
 
Kind regards,
 
Ruben



 



 

> Date: Wed, 25 Nov 2009 06:12:16 -0800
> From: [hidden email]
> Subject: Re: Macro for reordering variables
> To: [hidden email]
>
> Ruben van den Berg wrote:
> >
> >
> > Dear all,
> >
> >
> >
> > I'm trying to write a macro that should reorder the variables in my file.
> > Right now, I've Label.1 to Label.15 and then Leave.1 to Leave.15. I'd like
> > the order to be Label.1 Leave.1 Label.2 Leave.2 (...) Label.15 Leave.15.
> > Admittedly, I could write this by hand since it's only 30 variables but
> > next time it could be 100 or 200 variables and I'd really like to learn
> > some very basic macro language as well.
> >
> >
> >
> > Thanks a lot in advance!
> >
> >
> > define !reorder ()
> >
> > !do !t2=1 !to 15
> >
> > !let !varlist=!concat('label.',!t2) !concat('leave.',!t2)
> >
> > !doend
> >
> > matc fil fil *
> >
> > /kee variable_name !varlist
> >
> > exe.
> >
> > !end.
> >
> >
> >
> > set mpr on.
> >
> > !reorder.
> >
> >
>
> You need to append !varlist to itself each time through the loop, and you
> need to leave spaces between the variable names in the list. Try this:
>
> define !reorder ()
> !let !varlist=''
> !do !t2=1 !to 15
> !let !varlist=!concat(!varlist,' label.',!t2,' leave.',!t2)
> !doend
> match files file = *
> /keep variable_name !varlist
> exe.
> !end.
>
>
>
> -----
> --
> Bruce Weaver
> [hidden email]
> 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.
> --
> View this message in context: http://old.nabble.com/Macro-for-reordering-variables-tp26509696p26513307.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


Express yourself instantly with MSN Messenger! MSN Messenger
Reply | Threaded
Open this post in threaded view
|

Re: Macro for reordering variables

Jon K Peck
In reply to this post by Ruben Geert van den Berg

Writing the macro or Python code may be a useful exercise, but if you just want to alphabetize your variables and you have version 17? or 18, go to Data Editor Variable View, right click on the header of the Name column, and choose Sort from the pop-up menu.

Regards,
Jon Peck
-----------------
Sent from my BlackBerry Handheld.


  From: Ruben van den Berg [[hidden email]]
  Sent: 11/25/2009 09:25 AM GMT
  To: [hidden email]
  Subject: [SPSSX-L] Macro for reordering variables


Dear all,
 
I'm trying to write a macro that should reorder the variables in my file. Right now, I've Label.1 to Label.15 and then Leave.1 to Leave.15. I'd like the order to be Label.1 Leave.1 Label.2 Leave.2 (...) Label.15 Leave.15. Admittedly, I could write this by hand since it's only 30 variables but next time it could be 100 or 200 variables and I'd really like to learn some very basic macro language as well.
 
Thanks a lot in advance!

define !reorder ()
!do !t2=1 !to 15
!let !varlist=!concat('label.',!t2) !concat('leave.',!t2)
!doend
matc fil fil *
/kee variable_name !varlist
exe.
!end.
 
set mpr on.
!reorder.


 




Express yourself instantly with MSN Messenger! MSN Messenger