Repeating computations for multiple variable sets.

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

Repeating computations for multiple variable sets.

Koen
Hi!

I woild like to repeat the following computations for multiple variable sets. I have several variable groups, all following the pattern <Medname>_use <Medname>_start <Medname>_end, where <Medname> is replaced by the name for a medication. <Medname>_use classifies 2 types of medication usage (0, 1 or 2), <Medname>_start and <Medname>_date refer to dates when <Medname> treatment was started. I want to execute these command for all medicines I have in my data file, but I seem to get stuck. Any help would be greatly appreciated.

DO IF (ANY(Med1_use,1,2)).
COMPUTE Med1_start_days=DATEDIFF(Med1_start,dateindex,'days'). 
COMPUTE Med1_end_days=DATEDIFF(Med1_end,dateindex,'days').
END IF.
EXE.
Reply | Threaded
Open this post in threaded view
|

Re: Repeating computations for multiple variable sets.

Andy W
You can use a macro and pass a list of <Medname> base stubs. Example below.

****************************************************************.
data list free /dateindex Med1_start Med1_end Med2_start Med2_end (5ADATE8).
begin data
5/1/2013 6/1/2013 6/5/2013 6/7/2013 6/12/2013
4/1/2013 6/1/2013 6/5/2013 6/7/2013 6/12/2013
end data.

DEFINE !MedDiff (!POSITIONAL !CMDEND)
!DO !I !IN (!1)
compute !CONCAT(!I,"_StartDays") = DATEDIFF(!CONCAT(!I,"_start"),dateindex,'days').
compute !CONCAT(!I,"_EndDays")  = DATEDIFF(!CONCAT(!I,"_end"),dateindex,'days').
!DOEND
!ENDDEFINE.

!MedDiff Med1 Med2.
****************************************************************.

If you don't know the medicine base stub names before hand a python solution may be preferable.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Repeating computations for multiple variable sets.

Jon K Peck
If the variable names are reasonably static and there aren't too many of them, a macro definition will work fine, although there are some special syntax issues if you use these in CTABLES.

Otherwise, the SPSSINC SELECT VARIABLES extension command, which requires the Python Essentials, can be used to create macro definitions based, among other things, on pattern in the variable names such as all names ending with a particular string.  It can also address the CTABLES requirements.

This can be downloaded from the SPSS Community website (www.ibm.com/developerworks/spssdevcentral).


Jon Peck (no "h") aka Kim
Senior Software Engineer, IBM
[hidden email]
phone: 720-342-5621




From:        Andy W <[hidden email]>
To:        [hidden email],
Date:        07/12/2013 10:45 AM
Subject:        Re: [SPSSX-L] Repeating computations for multiple variable sets.
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




You can use a macro and pass a list of <Medname> base stubs. Example below.

****************************************************************.
data list free /dateindex Med1_start Med1_end Med2_start Med2_end (5ADATE8).
begin data
5/1/2013 6/1/2013 6/5/2013 6/7/2013 6/12/2013
4/1/2013 6/1/2013 6/5/2013 6/7/2013 6/12/2013
end data.

DEFINE !MedDiff (!POSITIONAL !CMDEND)
!DO !I !IN (!1)
compute !CONCAT(!I,"_StartDays") =
DATEDIFF(!CONCAT(!I,"_start"),dateindex,'days').
compute !CONCAT(!I,"_EndDays")  =
DATEDIFF(!CONCAT(!I,"_end"),dateindex,'days').
!DOEND
!ENDDEFINE.

!MedDiff Med1 Med2.
****************************************************************.

If you don't know the medicine base stub names before hand a python solution
may be preferable.



-----
Andy W
[hidden email]
http://andrewpwheeler.wordpress.com/
--
View this message in context:
http://spssx-discussion.1045642.n5.nabble.com/Repeating-computations-for-multiple-variable-sets-tp5721127p5721129.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


Reply | Threaded
Open this post in threaded view
|

Re: Repeating computations for multiple variable sets.

Koen
This post was updated on .
In reply to this post by Andy W
Great, thank you so much Andy, exactly what I had in mind. It works fine.

Could you help point me to where I have to put my IF expression in this macro? I'm not entirely sure what happens in the !DO !I !IN (!1) line.

My IF expression looks like !CONCAT("(ANY(",!I,"_use,1,2))"), I presume.
Reply | Threaded
Open this post in threaded view
|

Re: Repeating computations for multiple variable sets.

Koen
Found it myself, sorry.

For future reference:

****************************************************************.
DEFINE !MedDiff (!POSITIONAL !CMDEND)
!DO !I !IN (!1)
DO IF !CONCAT("(ANY(",!I,"_use,1,2))").
compute !CONCAT(!I,"_StartDays") = DATEDIFF(!CONCAT(!I,"_start"),dateindex,'days').
compute !CONCAT(!I,"_EndDays")  = DATEDIFF(!CONCAT(!I,"_end"),dateindex,'days').
!DOEND
!ENDDEFINE.

!MedDiff Med1 Med2.
****************************************************************.

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

Re: Repeating computations for multiple variable sets.

Andy W
In reply to this post by Koen
No, not quite. !CONCAT(!I,"_use") will expand to a string of whatever !I is plus "_use". So if I pass a list of names "Med1 Med2" to the macro, the first loop will expand to "Med1_use" and the second will expand to "Med2_use". You just then need to insert these strings into the appropriate places in the usual syntax.

****************************************************************.
data list free /dateindex Med1_start Med1_end Med2_start Med2_end (5ADATE8) Med1_use Med2_use (2F1.0).
begin data
5/1/2013 6/1/2013 6/5/2013 6/7/2013 6/12/2013 1 0
4/1/2013 6/1/2013 6/5/2013 6/7/2013 6/12/2013 0 2
end data.

DEFINE !MedDiff (!POSITIONAL !CMDEND)
!DO !I !IN (!1)
DO IF ANY(!CONCAT(!I,"_use"),1,2).
compute !CONCAT(!I,"_StartDays") = DATEDIFF(!CONCAT(!I,"_start"),dateindex,'days').
compute !CONCAT(!I,"_EndDays")  = DATEDIFF(!CONCAT(!I,"_end"),dateindex,'days').
END IF.
!DOEND
!ENDDEFINE.

!MedDiff Med1 Med2.
****************************************************************.

Sometimes for clarity it is easier to define the variables via a !LET command as opposed to multiple !CONCAT statements within the code (especially if you need to repetitively expand to the same string).
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: Repeating computations for multiple variable sets.

Andy W
You can of course have the strings expand to the full command (and your code will work) - but this IMO makes it harder to figure out what is the command and what is the variable.

You could include the "DO IF" part in the !CONCAT if you wanted to, but I see no real reason to do this.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/