How to use do repeat for no contigous variables?

Moderators: statman, Analyst Techy, andris, Fierce, GerineL, Smash

buh86
Posts: 5
Joined: Wed Jul 09, 2014 1:46 pm

How to use do repeat for no contigous variables?

Postby buh86 » Wed Jul 09, 2014 2:18 pm

Hello,

I have variables order like below:

a1 a2 a3 a4 b1 b2 b3 b4 c1 c2 c3 c4 ................. z1 z2 z3 z4

I have to sum up variables like below:

compute p5_a=sum(a1 to a4).
compute p5_b=sum(b1 to b4).
.
.
compute p5_z=sum(z1 to z4).

This kind of syntax is very long. I thought to use do repeat but it woul be also quite long like below:

do repeat
a=p5_a to p5_z
/b=a1 b1........................z1
/c=a2 b2........................z2
/d=a3 b3........................z3
/e=a4 b4........................z4.
compute a=sum(b,c,d,e).
end repeat.

Unfortunetly I am not able to write for example /b=a1 to z1 because variables are not contigous. Could someone has idea how to shorten this syntax? Unfortunetly, I do not have any idea (maybe reorder these variables like a1 b1 c1......z1 a2 b2......z2 a3...............etc. but how can I do it quite fast without very long syntax, or maybe use loop???? )

Thank you for help in advance.
GerineL
Moderator
Posts: 1477
Joined: Tue Jun 10, 2008 4:50 pm

Re: How to use do repeat for no contigous variables?

Postby GerineL » Wed Jul 09, 2014 3:27 pm

don't know, unfortunately.
I just type all variable names (or copy/paste them).

Alternatively, you could think about using excel to create a syntax to rename all your variables, and then use the renamed variables in your do repeats?
buh86
Posts: 5
Joined: Wed Jul 09, 2014 1:46 pm

Re: How to use do repeat for no contigous variables?

Postby buh86 » Wed Jul 09, 2014 3:59 pm

Yes, but what when I have about 1000 set of variables like that. Syntax is really huge. I think that is possible to reorder variables from
a1 a2 a3 a4 b1 b2 b3 b4 c1 c2 c3 c4 ................. z1 z2 z3 z4
to
a1 b1 c1......z1 a2 b2......z2 a3...............etc.
using makro or loop but I do not know how yet.
RubenGeert
Posts: 100
Joined: Mon May 19, 2014 6:06 am

Re: How to use do repeat for no contigous variables?

Postby RubenGeert » Thu Jul 10, 2014 6:57 am

For SPSS version 16 and higher, run

SORT VARIABLES BY NAME (A).

which should render relevant variables adjacent if your variable names are as you described. If that doesn't work, there are some alternatives:

1) Use a tiny Python script such as this one: http://www.spss-tutorials.com/select-va ... -in-names/. Something like this may actually be (very) useful for your situation anyway.

2) Use the old school hack of syntax generating syntax. I'll only go into that if everything else really fails because I feel it's a bit on an emergency solution.

Let me know whether that gets you any further because there's plenty of options for such situations.

Kind regards,

Ruben Geert van den Berg
www.spss-tutorials.com
buh86
Posts: 5
Joined: Wed Jul 09, 2014 1:46 pm

Re: How to use do repeat for no contigous variables?

Postby buh86 » Thu Jul 10, 2014 12:23 pm

Thank you for help.

I am going to learn Python script in the near future.

I have also found the macro which reorder variables : http://www.spsstools.net/Macros.htm .
RubenGeert
Posts: 100
Joined: Mon May 19, 2014 6:06 am

Re: How to use do repeat for no contigous variables?

Postby RubenGeert » Thu Jul 10, 2014 7:40 pm

Just curiosity: exactly which macro did you use and why didn't SORT VARIABLES not work? Did you get the job done yet or do you need further assistance?

Don't mind me for asking but I find your's an interesting case. If there's any specific difficulties I can perhaps write a tutorial on it.

Kind regards,

Ruben Geert van den Berg
www.spss-tutorials.com
buh86
Posts: 5
Joined: Wed Jul 09, 2014 1:46 pm

Re: How to use do repeat for no contigous variables?

Postby buh86 » Fri Jul 11, 2014 11:19 am

I use macro reorder vector names, last macro on site.

Unfortunately, sort variables did not work because sort variables like that: a1 a2 b1 b2 and I need a1 b1 a2 b2.

Best Regards,

Maciek
RubenGeert
Posts: 100
Joined: Mon May 19, 2014 6:06 am

Re: How to use do repeat for no contigous variables?

Postby RubenGeert » Fri Jul 11, 2014 2:58 pm

Hi Maciek,

Interesting indeed! Do you already have Python installed? If so, try this syntax: http://spss-tutorials.com/downloads/reo ... iables.sps

I'll write a tutorial on this and a couple of similar cases (sorting by second subscript for instance).

Thanks for sharing the info!

Ruben

Who is online

Users browsing this forum: No registered users and 1 guest

cron