Variable Variable Names

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

Variable Variable Names

Matt Freeman
I've got some syntax for which it would be quite convenient to define a
variable that indicates the number of variables to loop through in a DO REPEAT
loop.  Consider as an example the following syntax for a situation in which I
have 3310 variables:

DO REPEAT T = T1 TO T3310 / X = 1 TO 3310.
COMPUTE T = LN(X).
END REPEAT.

I have quite a few lines of syntax that depend on the value of 3310 and would
like to have a way to change this value only once.  Is it possible to write
something such as

COMPUTE NUM = 3310.
DO REPEAT T = T1 TO TNUM / X = 1 TO NUM.
COMPUTE T = LN(X).
END REPEAT.

Any ideas?  Thanks!

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

Re: Variable Variable Names

Bruce Weaver
Administrator
Have you tried sticking your DO REPEAT inside a macro that takes the number of variables as an argument?  I don't have SPSS installed on this machine, so the following is untested.

DEFINE MyLoop (NumVars = !tokens(1))

DO REPEAT T = T1 TO !CONCAT("T",!NumVars) / X = 1 TO !NumVars.
COMPUTE T = LN(X).
END REPEAT.

!ENDDEFINE.

* Call the macro.
MyLoop NumVars = 3310.


Matt Freeman wrote
I've got some syntax for which it would be quite convenient to define a
variable that indicates the number of variables to loop through in a DO REPEAT
loop.  Consider as an example the following syntax for a situation in which I
have 3310 variables:

DO REPEAT T = T1 TO T3310 / X = 1 TO 3310.
COMPUTE T = LN(X).
END REPEAT.

I have quite a few lines of syntax that depend on the value of 3310 and would
like to have a way to change this value only once.  Is it possible to write
something such as

COMPUTE NUM = 3310.
DO REPEAT T = T1 TO TNUM / X = 1 TO NUM.
COMPUTE T = LN(X).
END REPEAT.

Any ideas?  Thanks!

=====================
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
--
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: Variable Variable Names

Andy W
In reply to this post by Matt Freeman
See DEFINE-!ENDDEFINE in the manual (i.e. the macro facility). You would just have 3310 as a parameter to pass. Also if the do repeat is exactly the same you could pass a list of base variable names. A quick untested example might look like this;

DEFINE !do_rep (n = !TOKENS(1)
                       /base_name = !CMDEND) ).

!DO !iter !IN (!base_name)

!LET !beg_rep = !CONCAT(!iter,"1")
!LET !end_rep = !CONCAT(!iter,!n)

DO REPEAT var = !beg_rep TO !end_rep / X = 1 TO !n.
COMPUTE var = LN(X).
END REPEAT.

!DOEND

!ENDDEFINE.

Then below will expand to what you have written plus a second list of variables with S as the base prefix (i.e. S1 to S3310).

set mprint on.
!do_rep n = 3310 base_name = T S.

As the usual harking, do you really have 3310 variables? And if this is an actual use case (and not just a trivial example) this writing of actual variable values to the data matrix wouldn't make any sense in any workflow that I can imagine. If you have multiple variable sets with 3310 variables you might want to reconsider your workflow to switching the data format from wide to long to perform such operations. My 2cents.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Variable Variable Names

David Marso
Administrator
In reply to this post by Matt Freeman
Assuming this is related to your CURVEFIT issue from last week.
Consider a MATRIX implementation where such becomes irrelevant.
data list list / y1 to y10.
begin data
 1 36  1  6 31  3 16 31 72 35
63 61  3 61 25  3  6  1  6  3
 5  1  3  5  1  2  6 35  6  1
25  6 12  5  6 13  1  6  3  5
12  6  5  3  6  2  5  3 65 16
 2  3  7  6  1 73 11 25 13 32
end data.
COMPUTE T=$CASENUM.
MATRIX.
GET Y / FILE * / VAR Y1 TO Y10.
GET X / FILE * / VAR T.
COMPUTE LY=LN(Y).
*PRINT LY.
COMPUTE X={MAKE(NROW(X),1,1),X}.
COMPUTE BHat=GINV(T(X)*X)*T(X)*LY.
COMPUTE BHat(1,:)=EXP(BHat(1,:)).
PRINT BHat.
END MATRIX.
CURVEFIT
* Curve Estimation to check results *.
TSET NEWVAR=NONE .
CURVEFIT /VARIABLES=y1 y2 y3 y4 y5 y6 y7 y8 y9 y10  WITH t
  /CONSTANT
  /MODEL=EXPONENTIAL
  /PLOT FIT.

Matt Freeman wrote
I've got some syntax for which it would be quite convenient to define a
variable that indicates the number of variables to loop through in a DO REPEAT
loop.  Consider as an example the following syntax for a situation in which I
have 3310 variables:

DO REPEAT T = T1 TO T3310 / X = 1 TO 3310.
COMPUTE T = LN(X).
END REPEAT.

I have quite a few lines of syntax that depend on the value of 3310 and would
like to have a way to change this value only once.  Is it possible to write
something such as

COMPUTE NUM = 3310.
DO REPEAT T = T1 TO TNUM / X = 1 TO NUM.
COMPUTE T = LN(X).
END REPEAT.

Any ideas?  Thanks!

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Variable Variable Names

Matt Freeman
In reply to this post by Matt Freeman
Thanks for the work gentlemen.  I certainly appreciate it.

Andy - The reason I have so many variables is because I've transposed my
data set.  I really have 3,310 cases and only 10 variables.  I'm doing
3,310 regressions and capturing the parameter estimates for each one.  The
next time I do this, I'd like to be able to just change the number of
variables.

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

Re: Variable Variable Names

David Marso
Administrator
Note that with the MATRIX solution there is NO looping involved and no need to know the number of variables.
Matt Freeman wrote
Thanks for the work gentlemen.  I certainly appreciate it.

Andy - The reason I have so many variables is because I've transposed my
data set.  I really have 3,310 cases and only 10 variables.  I'm doing
3,310 regressions and capturing the parameter estimates for each one.  The
next time I do this, I'd like to be able to just change the number of
variables.

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: Variable Variable Names

Matt Freeman
In reply to this post by Matt Freeman
Thanks David! I just implemented the matrix solution and it is quite
elegant.  I've learned a lot from you about SPSS syntax over the past
couple of weeks and I am truly grateful.

Andy and Bruce - I will be reviewing your suggestions as well, as I have
just recently started learning about macros.  I am grateful for your time
and efforts as well.

Thanks!

Matt

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

Re: Variable Variable Names

David Marso
Administrator
FWIW:  Here is an even more compact version.  You should look at the SAVE subcommand in MATRIX as well.

MATRIX.
GET Y / FILE * / VAR Y1 TO Y10.
GET X / FILE * / VAR ONE T.
COMPUTE BHat=GINV(T(X)*X)*T(X)*LN(Y).
COMPUTE BHat(1,:)=EXP(BHat(1,:)).
PRINT T(BHat).
END MATRIX.

Matt Freeman wrote
Thanks David! I just implemented the matrix solution and it is quite
elegant.  I've learned a lot from you about SPSS syntax over the past
couple of weeks and I am truly grateful.

Andy and Bruce - I will be reviewing your suggestions as well, as I have
just recently started learning about macros.  I am grateful for your time
and efforts as well.

Thanks!

Matt

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"