Loop vars

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

Loop vars

Jignesh Sutar
Hello
 
Simple looping question:
 
vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"
 
How do I create a macro such that it creates;
 
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9 v10.
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9.
FREQ v1 v2 v3 v4 v5 v6 v7 v8.
FREQ v1 v2 v3 v4 v5 v6 v7.
FREQ v1 v2 v3 v4 v5 v6 .
FREQ v1 v2 v3 v4 v5.
 
If I could set a limit to the total number of variables removed after all iterations that would be handy.
 
Ideally, looking for a non-python solution at the moment but a python solution would be good for reference for future reference...
 
 
TIA
Jignesh
 
Reply | Threaded
Open this post in threaded view
|

Re: Loop vars

ViAnn Beadle

Why would you want this?

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of J Sutar
Sent: Monday, November 01, 2010 5:04 PM
To: [hidden email]
Subject: Loop vars

 

Hello

 

Simple looping question:

 

vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"

 

How do I create a macro such that it creates;

 

FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9 v10.

FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9.

FREQ v1 v2 v3 v4 v5 v6 v7 v8.

FREQ v1 v2 v3 v4 v5 v6 v7.

FREQ v1 v2 v3 v4 v5 v6 .

FREQ v1 v2 v3 v4 v5.

 

If I could set a limit to the total number of variables removed after all iterations that would be handy.

 

Ideally, looking for a non-python solution at the moment but a python solution would be good for reference for future reference...

 

 

TIA

Jignesh

 

Reply | Threaded
Open this post in threaded view
|

Re: Loop vars

Jignesh Sutar

My actually code wouldn't be running this on frequencies but on discriminate analysis. I've just presented it as frequencies to simplify the question.

Why I'm looping like this is part if a bigger picture which essentially is trying to find a optimal solution, testing different number of variables and calculating further statistics from the back of the results.

On 2 Nov 2010 00:38, "ViAnn Beadle" <[hidden email]> wrote:

Why would you want this?

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of J Sutar
Sent: Monday, November 01, 2010 5:04 PM
To: [hidden email]
Subject: Loop vars



 

Hello

 

Simple looping question:

 

vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"

 

How do I creat...

Reply | Threaded
Open this post in threaded view
|

Re: Loop vars

Albert-Jan Roskam
In reply to this post by Jignesh Sutar

Hi,

 

Using the sample file 'anorectic.sav' (see the spss install dir):

 

begin program.

import spss

vars = "weight mens fast binge vomit".split()

for group in [vars[0: len(vars) - i] for i in range(len(vars))]:

    spss.Submit("fre %s." % (" ".join(group)))

end program.

 

I don't know how to do this using the macro language. Maybe with !head and !tail. Macros are evil anyway. ;-)


 
Cheers!!
Albert-Jan


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



From: J Sutar <[hidden email]>
To: [hidden email]
Sent: Tue, November 2, 2010 12:03:34 AM
Subject: [SPSSX-L] Loop vars

Hello
 
Simple looping question:
 
vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"
 
How do I create a macro such that it creates;
 
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9 v10.
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9.
FREQ v1 v2 v3 v4 v5 v6 v7 v8.
FREQ v1 v2 v3 v4 v5 v6 v7.
FREQ v1 v2 v3 v4 v5 v6 .
FREQ v1 v2 v3 v4 v5.
 
If I could set a limit to the total number of variables removed after all iterations that would be handy.
 
Ideally, looking for a non-python solution at the moment but a python solution would be good for reference for future reference...
 
 
TIA
Jignesh
 

Reply | Threaded
Open this post in threaded view
|

Re: Loop vars

Bruce Weaver
Administrator
In reply to this post by Jignesh Sutar
Have you seen Mike Babyak's nice article on over-fitting of regression models?  It might make you rethink your strategy.  You can download a PDF here:

    http://www.class.uidaho.edu/psy586/Course%20Readings/Babyak_04.pdf

HTH.


J Sutar wrote
My actually code wouldn't be running this on frequencies but on discriminate
analysis. I've just presented it as frequencies to simplify the question.

Why I'm looping like this is part if a bigger picture which essentially is
trying to find a optimal solution, testing different number of variables and
calculating further statistics from the back of the results.

On 2 Nov 2010 00:38, "ViAnn Beadle" <vab88011@gmail.com> wrote:

Why would you want this?



*From:* SPSSX(r) Discussion [mailto:SPSSX-L@LISTSERV.UGA.EDU] *On Behalf Of
*J Sutar
*Sent:* Monday, November 01, 2010 5:04 PM
*To:* SPSSX-L@LISTSERV.UGA.EDU
*Subject:* Loop vars





Hello



Simple looping question:



vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"



How do I creat...
--
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: Loop vars

Albert-Jan Roskam
In reply to this post by Jignesh Sutar
Hi,

On second thought, it might be slightly simpler using list striding (I always forget about that):
vars = "v1 v2 v3 v4 v5 v6 v7 v8 v9 v10".split()
STOP = 6
for n, var in enumerate(vars):
    cmd = "fre %s." % (" ".join(vars[n::]))
    spss.Submit(cmd)
    if n + 1 == STOP:
        break

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 11/2/10, J Sutar <[hidden email]> wrote:

From: J Sutar <[hidden email]>
Subject: [SPSSX-L] Loop vars
To: [hidden email]
Date: Tuesday, November 2, 2010, 12:03 AM

Hello
 
Simple looping question:
 
vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"
 
How do I create a macro such that it creates;
 
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9 v10.
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9.
FREQ v1 v2 v3 v4 v5 v6 v7 v8.
FREQ v1 v2 v3 v4 v5 v6 v7.
FREQ v1 v2 v3 v4 v5 v6 .
FREQ v1 v2 v3 v4 v5.
 
If I could set a limit to the total number of variables removed after all iterations that would be handy.
 
Ideally, looking for a non-python solution at the moment but a python solution would be good for reference for future reference...
 
 
TIA
Jignesh
 

Reply | Threaded
Open this post in threaded view
|

Re: Loop vars

Jignesh Sutar
Hi,
 
Ok....I've wrecked my brain to the Nth degree to try debug this macro below. Python solution works for me but am trying to replicate using conventional spss syntax. I'm trying to generate a varaible list which drops one variable each time. The macro below works perfect when dropping 1 to 8 variables but anything after 9 it suprisingly doesnt behave as expected....? I'm confused.
 
/*////////////////////////////////////////*/.
/* LOAD (!dropOneVarList) MACRO */.
/*////////////////////////////////////////*/.
define !dropOneVarList (vars=!charend('/')
  /nDrop=!charend('/'))
 
!let !h=!head(!vars)
!let !t=!tail(!vars)
!let !reverseVarList=!h
!do !i !in (!t)
  !let !reverseVarList=!concat(!i,' ',!reverseVarList)
!doend
 
!let !droppedVarList=!vars
!let !dropped=!blanks(0)
!do !i !in (!reverseVarList)
  !let !dropped=!concat(!dropped,!blanks(1))
  !if (!len(!dropped) <= !nDrop)
  !then
  !let !droppedVarList=!substr( !droppedVarList,1, !length(!substr(!blanks(!index(!droppedVarList,!i)), !length(!concat(!blanks(1), !blanks(2))))))
  document dropped=!len(!dropped); droppedVarList=!droppedVarList.
  !else
  !break
  !ifend
!doend
 
display document.
!enddefine.
/*////////////////////////////////////////*/.
/* END (!dropOneVarList) MACRO */.
/*////////////////////////////////////////*/. 
 
set mprint on.
!dropOneVarList vars=V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 /nDrop=8.
set mprint off.
 
Many thanks in advance
Jignesh

 
On 3 November 2010 17:07, Albert-Jan Roskam <[hidden email]> wrote:
Hi,

On second thought, it might be slightly simpler using list striding (I always forget about that):
vars = "v1 v2 v3 v4 v5 v6 v7 v8 v9 v10".split()
STOP = 6
for n, var in enumerate(vars):
    cmd = "fre %s." % (" ".join(vars[n::]))
    spss.Submit(cmd)
    if n + 1 == STOP:
        break

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 11/2/10, J Sutar <[hidden email]> wrote:

From: J Sutar <[hidden email]>
Subject: [SPSSX-L] Loop vars
Date: Tuesday, November 2, 2010, 12:03 AM


Hello
 
Simple looping question:
 
vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"
 
How do I create a macro such that it creates;
 
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9 v10.
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9.
FREQ v1 v2 v3 v4 v5 v6 v7 v8.
FREQ v1 v2 v3 v4 v5 v6 v7.
FREQ v1 v2 v3 v4 v5 v6 .
FREQ v1 v2 v3 v4 v5.
 
If I could set a limit to the total number of variables removed after all iterations that would be handy.
 
Ideally, looking for a non-python solution at the moment but a python solution would be good for reference for future reference...
 
 
TIA
Jignesh
 


Reply | Threaded
Open this post in threaded view
|

Re: Loop vars

Jignesh Sutar
Hi,
 
Anyone able to help with this?
 
Just to break down the steps for the macro:
 
1. Reversing the ordering of the original list from V1 to V11 to V11 to V1.
2. Looping for i in V11 to V1.
3. String substituting  the end most variable each time so gradually dropping one variable each time.
Please see my very first post if still unclear.
 
The macro works fine but its only when I specify I want to drop 9 or more variable that it all of a sudden does not behave as expected...
 
 
Many thanks.
Jignesh
 
 
On 27 November 2010 11:17, J Sutar <[hidden email]> wrote:
Hi,
 
Ok....I've wrecked my brain to the Nth degree to try debug this macro below. Python solution works for me but am trying to replicate using conventional spss syntax. I'm trying to generate a varaible list which drops one variable each time. The macro below works perfect when dropping 1 to 8 variables but anything after 9 it suprisingly doesnt behave as expected....? I'm confused.
 
/*////////////////////////////////////////*/.
/* LOAD (!dropOneVarList) MACRO */.
/*////////////////////////////////////////*/.
define !dropOneVarList (vars=!charend('/')
  /nDrop=!charend('/'))
 
!let !h=!head(!vars)
!let !t=!tail(!vars)
!let !reverseVarList=!h
!do !i !in (!t)
  !let !reverseVarList=!concat(!i,' ',!reverseVarList)
!doend
 
!let !droppedVarList=!vars
!let !dropped=!blanks(0)
!do !i !in (!reverseVarList)
  !let !dropped=!concat(!dropped,!blanks(1))
  !if (!len(!dropped) <= !nDrop)
  !then
  !let !droppedVarList=!substr( !droppedVarList,1, !length(!substr(!blanks(!index(!droppedVarList,!i)), !length(!concat(!blanks(1), !blanks(2))))))
  document dropped=!len(!dropped); droppedVarList=!droppedVarList.
  !else
  !break
  !ifend
!doend
 
display document.
!enddefine.
/*////////////////////////////////////////*/.
/* END (!dropOneVarList) MACRO */.
/*////////////////////////////////////////*/. 
 
set mprint on.
!dropOneVarList vars=V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 /nDrop=8.
set mprint off.
 
Many thanks in advance
Jignesh

 
On 3 November 2010 17:07, Albert-Jan Roskam <[hidden email]> wrote:
Hi,

On second thought, it might be slightly simpler using list striding (I always forget about that):
vars = "v1 v2 v3 v4 v5 v6 v7 v8 v9 v10".split()
STOP = 6
for n, var in enumerate(vars):
    cmd = "fre %s." % (" ".join(vars[n::]))
    spss.Submit(cmd)
    if n + 1 == STOP:
        break

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 11/2/10, J Sutar <[hidden email]> wrote:

From: J Sutar <[hidden email]>
Subject: [SPSSX-L] Loop vars
Date: Tuesday, November 2, 2010, 12:03 AM


Hello
 
Simple looping question:
 
vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"
 
How do I create a macro such that it creates;
 
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9 v10.
FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9.
FREQ v1 v2 v3 v4 v5 v6 v7 v8.
FREQ v1 v2 v3 v4 v5 v6 v7.
FREQ v1 v2 v3 v4 v5 v6 .
FREQ v1 v2 v3 v4 v5.
 
If I could set a limit to the total number of variables removed after all iterations that would be handy.
 
Ideally, looking for a non-python solution at the moment but a python solution would be good for reference for future reference...
 
 
TIA
Jignesh
 



Reply | Threaded
Open this post in threaded view
|

Re: Loop vars

Robert Walker

Jignesh,

 

Try this:

 

DATA LIST FREE / V1 TO V10 (10F1).

BEGIN DATA

1 2 3 4 1 2 3 4 1 0

2 9 3 6 2 5 6 9 2 5

5 4 0 4 1 2 3 4 9 0

END DATA.

 

SET MPRINT ON.

SET MEXPAND ON.

SET PRINTBACK ON.

 

DEFINE !SLICER().

!LET !LOW=1 !LET !HIGH=10

 

!DO !Z = !HIGH !TO !LOW !BY -1

FREQUENCIES ALL.

MATCH FILES /FILE=* /DROP !CONCAT("V",!Z).

EXECUTE.

!DOEND

 

!ENDDEFINE.

!SLICER.

 

Bob Walker

Surveys & Forecasts, LLC

www.safllc.com

 

From: SPSSX(r) Discussion [mailto:[hidden email]] On Behalf Of J Sutar
Sent: Friday, December 03, 2010 4:33 AM
To: [hidden email]
Subject: Re: Loop vars

 

Hi,

 

Anyone able to help with this?

 

Just to break down the steps for the macro:

 

1. Reversing the ordering of the original list from V1 to V11 to V11 to V1.

2. Looping for i in V11 to V1.

3. String substituting  the end most variable each time so gradually dropping one variable each time.

Please see my very first post if still unclear.

 

The macro works fine but its only when I specify I want to drop 9 or more variable that it all of a sudden does not behave as expected...

 

 

Many thanks.

Jignesh

 

 

On 27 November 2010 11:17, J Sutar <[hidden email]> wrote:

Hi,

 

Ok....I've wrecked my brain to the Nth degree to try debug this macro below. Python solution works for me but am trying to replicate using conventional spss syntax. I'm trying to generate a varaible list which drops one variable each time. The macro below works perfect when dropping 1 to 8 variables but anything after 9 it suprisingly doesnt behave as expected....? I'm confused.

 

/*////////////////////////////////////////*/.
/* LOAD (!dropOneVarList) MACRO */.
/*////////////////////////////////////////*/.
define !dropOneVarList (vars=!charend('/')
  /nDrop=!charend('/'))

 

!let !h=!head(!vars)
!let !t=!tail(!vars)
!let !reverseVarList=!h

!do !i !in (!t)
  !let !reverseVarList=!concat(!i,' ',!reverseVarList)
!doend

 

!let !droppedVarList=!vars
!let !dropped=!blanks(0)
!do !i !in (!reverseVarList)
  !let !dropped=!concat(!dropped,!blanks(1))
  !if (!len(!dropped) <= !nDrop)
  !then
  !let !droppedVarList=!substr( !droppedVarList,1, !length(!substr(!blanks(!index(!droppedVarList,!i)), !length(!concat(!blanks(1), !blanks(2))))))
  document dropped=!len(!dropped); droppedVarList=!droppedVarList.
  !else
  !break
  !ifend
!doend

 

display document.
!enddefine.
/*////////////////////////////////////////*/.
/* END (!dropOneVarList) MACRO */.
/*////////////////////////////////////////*/. 

 

set mprint on.
!dropOneVarList vars=V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 /nDrop=8.
set mprint off.

 

Many thanks in advance

Jignesh

 

On 3 November 2010 17:07, Albert-Jan Roskam <[hidden email]> wrote:

Hi,

On second thought, it might be slightly simpler using list striding (I always forget about that):
vars = "v1 v2 v3 v4 v5 v6 v7 v8 v9 v10".split()
STOP = 6
for n, var in enumerate(vars):
    cmd = "fre %s." % (" ".join(vars[n::]))
    spss.Submit(cmd)
    if n + 1 == STOP:
        break


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 11/2/10, J Sutar <[hidden email]> wrote:


From: J Sutar <[hidden email]>

Subject: [SPSSX-L] Loop vars

Date: Tuesday, November 2, 2010, 12:03 AM

 

Hello

 

Simple looping question:

 

vars="v1 v2 v3 v4 v5 v6 v7 v8 v9 v10"

 

How do I create a macro such that it creates;

 

FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9 v10.

FREQ v1 v2 v3 v4 v5 v6 v7 v8 v9.

FREQ v1 v2 v3 v4 v5 v6 v7 v8.

FREQ v1 v2 v3 v4 v5 v6 v7.

FREQ v1 v2 v3 v4 v5 v6 .

FREQ v1 v2 v3 v4 v5.

 

If I could set a limit to the total number of variables removed after all iterations that would be handy.

 

Ideally, looking for a non-python solution at the moment but a python solution would be good for reference for future reference...

 

 

TIA

Jignesh