|
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 |
|
Administrator
|
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/). |
|
In reply to this post by Ruben Geert van den Berg
|
|
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)… From: SPSSX(r) Discussion
[mailto:[hidden email]] On Behalf Of Ruben van den Berg Dear all, Express
yourself instantly with MSN Messenger! MSN
Messenger |
|
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 |
|
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. From: Ruben van den Berg [[hidden email]] 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 |
| Free forum by Nabble | Edit this page |
