changing variable name dynamically to compute

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

changing variable name dynamically to compute

GauravSrivastava
Hi,

I have a set of syntax which I have to replicate 10-15 time by changing some part (.IN) in variable name. below is the syntax.

COMPUTE ResearchOffline=0.
IF any(1,Q.15_01.RE.IN to Q.15_12.RE.IN)  ResearchOffline=1.

COMPUTE ResearchOnline=0.
IF any(1,Q.15_13.RE.IN to Q.15_33.RE.IN) ResearchOnline=1.

COMPUTE NoResearch=0.
IF Q.15_34.RE.IN = 1   NoResearch=1.
EXECUTE.

I have to change this ".IN" part to ".AU", ".BC", ".CN", etc.

I am not sure If i can do this using concat or do repeat. Can anyone help me on this.

Thanks for your help.

Gaurav
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

Jignesh Sutar
Look up DEFINE/!ENDDEFINE

Use a List-Processing Loop to loop over AU/BC/CN ect  

And then !CONCAT to build your string variable.

Plenty of examples in archives to help you achieve this.


Similar logic can be applied using Python.

begin program.
import spss
for i in ["AU", "BC", "CN"]
    print """compute Test%(i)s="%(i)s". """" % locals()
    spss.Submit("""compute Test%(i)s="%(i)s". """" % locals())
end program.


Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

GauravSrivastava
Thanks Jignesh,

I have a bit tight hand with SPSS macro, Pythan. If you can add some examples, would be helpful.

Thanks again
Gaurav
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

Jignesh Sutar
Happy to add an example to get you going (see below) but you may want to consider investing some time reading the documentation that is shipped with the software (and make a habit of this) as it is a very useful resource to learn new things (it seems it is not the first time you've required a solution which has required the use of DEFINE/!ENDDEFINE).

DEFINE/!ENDDEFINE is somewhat a complicated beast to get started with but again there are many, many examples in the archives/google search that you can learn, beyond what the documentation provides also.


DEFINE !RunJob ()
!DO !i !IN ("AC BC DC")
    STRING !CONCAT("TEST_", !i) (A2).
    COMPUTE !CONCAT("TEST_", !i)=!QUOTE(!i).
!DOEND
!ENDDEFINE.

SET MPRINT ON.
!RunJob
SET MPRINT OFF.
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

GauravSrivastava
In reply to this post by GauravSrivastava
Thanks Jignesh,

Yes, you are write, this is not first time when I am struggling with macro stuff. I have few questions:

1. Will If command work under Define/!Do.
2. My variables are numeric and I have to perform logical expression (and/or/eq) here. since concat can be used for string variable. Will it work.

Thanks
Gaurav
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

GauravSrivastava
Here is is part of my syntax:

COMPUTE Q.15.ResearchOffline=0.
IF
Q.15_01.RE.IN =1 or
Q.15_02.RE.IN =1 or
Q.15_03.RE.IN =1 or
Q.15_04.RE.IN =1 or
Q.15_05.RE.IN =1 or
Q.15_06.RE.IN =1 or
Q.15_07.RE.IN =1 or
Q.15_08.RE.IN =1 or
Q.15_09.RE.IN =1 or
Q.15_10.RE.IN =1 or
Q.15_11.RE.IN =1 or
Q.15_12.RE.IN =1
Q.15.ResearchOffline=1.
EXECUTE.

I have to replace 'IN' with 'BN', 'BP'. I am trying but concat is not working here.

Thanks!
Gaurav
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

Jignesh Sutar
DEFINE/!ENDDEFINE is simply used to generate/build the syntax that you intended to run so it can be generated for all commands including IF command.

!CONCAT in this context (MACRO language) is not the concatenation of string variables in the dataset but the concatenation of string text which is being used to build the macro under DEFINE/!ENDDEFINE. You should understand the distinguish between this and the string manipulation function CONCAT.


I don't see any of your failed attempts to get this working? Please perhaps post your attempts at first.


Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

Mario Giesel
Hi, Gaurav,
  something like this will probably do the job  (untested):

* ==========================================================================.
* Macro definition.
DEFINE @calc(key = !POS !TOKENS(1) /name1 = !POS !TOKENS(1) /name2 = !POS !TOKENS(1) /name3 = !POS !TOKENS(1))
COMPUTE !name1=0.
IF any(1,!CONCAT('Q.15_01.RE.',!key) to !CONCAT("Q.15_12.RE.",!key))  !name1=1.
COMPUTE !name2=0.
IF any(1,!CONCAT("Q.15_13.RE.",!key) to !CONCAT("Q.15_33.RE.",!key) !name2=1.
COMPUTE !name3=0.
IF (!CONCAT("Q.15_34.RE.",!key") = 1)  !name3=1.
EXECUTE.
!ENDDEFINE.

* Macro call.
@calc key = IN name1 = ResearchOffline name2 = ResearchOnline name3 = NoResearch.
* ==========================================================================.

GL,
  Mario



Jignesh Sutar <[hidden email]> schrieb am 13:24 Montag, 1.Juni 2015:


DEFINE/!ENDDEFINE is simply used to generate/build the syntax that you
intended to run so it can be generated for all commands including IF
command.

!CONCAT in this context (MACRO language) is not the concatenation of *string
variables* in the dataset but the concatenation of string text which is
being used to build the macro under DEFINE/!ENDDEFINE. You should understand
the distinguish between this and the string manipulation function CONCAT.


I don't see any of your failed attempts to get this working? Please perhaps
post your attempts at first.






--
View this message in context: http://spssx-discussion.1045642.n5.nabble.com/changing-variable-name-dynamically-to-compute-tp5729658p5729668.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


===================== 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
Mario Giesel
Munich, Germany
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

Jignesh Sutar
By the way, you can create dichotomous variables with a single COMPUTE command.

COMPUTE ResearchOffline=any(1,Q.15_01.RE.IN to Q.15_12.RE.IN).

Where if the right hand side of the equation evaluates to be TRUE then a 1 (one) is computed else a 0 (zero) if FALSE.

PS. I don't think your macro definition answers the OP question in that he is wanting to loop over IN, AU, BC, CN. Also I don't see the need to parametrize the variable names "ResearchOffline"/"ResearchOnline" & "NoResearch"?
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

GauravSrivastava
In reply to this post by GauravSrivastava
Thanks for letting me know. I help me to clear my concept and it's working fine.

Below is the some part of it.

DEFINE !createvariables1 ()
!DO !i !IN ("CL CE GS HO PC HL AU HF  DG TR BP BN IN")
  IF any (1,!concat('Q.15_01.RE.',!i),!concat('Q.15_02.RE.',!i),!concat('Q.15_03.RE.',!i),!concat('Q.15_04.RE.',!i),!concat('Q.15_05.RE.',!i)
  ,!concat('Q.15_06.RE.',!i),!concat('Q.15_07.RE.',!i),!concat('Q.15_08.RE.',!i),!concat('Q.15_09.RE.',!i),!concat('Q.15_10.RE.',!i)
  ,!concat('Q.15_11.RE.',!i),!concat('Q.15_12.RE.',!i))  Q.15.ResearchOffline=1.
 
  IF any (1,!concat('Q.15_13.RE.',!i),!concat('Q.15_14.RE.',!i),!concat('Q.15_15.RE.',!i),!concat('Q.15_16.RE.',!i),!concat('Q.15_17.RE.',!i)
    ,!concat('Q.15_18.RE.',!i),!concat('Q.15_19.RE.',!i),!concat('Q.15_20.RE.',!i),!concat('Q.15_21.RE.',!i),!concat('Q.15_22.RE.',!i)
  ,!concat('Q.15_23.RE.',!i),!concat('Q.15_24.RE.',!i),!concat('Q.15_25.RE.',!i),!concat('Q.15_26.RE.',!i),!concat('Q.15_27.RE.',!i)
  ,!concat('Q.15_28.RE.',!i),!concat('Q.15_29.RE.',!i),!concat('Q.15_30.RE.',!i),!concat('Q.15_31.RE.',!i),!concat('Q.15_33.RE.',!i))  Q.15.ResearchOnline=1.
 
  IF (!concat('Q.15_34.RE.',!i) = 1)  Q.15.NoResearch=1.
!doend
!enddefine.



Thanks again.
Gaurav
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

Jignesh Sutar
You may want to consider restructuring your file so those set of 13 variables as stacked (VARSTOCASES). So instead of having 13 sets of variables defining these groups you have this as a variable in the dataset.

I suspect this may be a more efficient, better approach to work with the data you have but it all depends on your next step(s) and end goal.
Reply | Threaded
Open this post in threaded view
|

Re: changing variable name dynamically to compute

GauravSrivastava
Thankyou both,

Jignesh, My data is stored in both the way vertical as well as width wise. sometime i need to use it individually.

Thanks for your help.

Gaurav