conditional compute within a macro

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

conditional compute within a macro

Beckstead, Jason

Listers,

I am trying to compute a lagged variable within a macro using CONDITIONAL, !IF !THEN, but having no luck.

can anyone offer assistance? Below is simplified data set/structure and current macro attempt.

 

Thanks.

 

DATA LIST FREE/ ID DAY X.

BEGIN DATA.

1 1 10

1 2 15

1 3 20

1 4 25

1 5 30

2 1 20

2 2 21

2 3 22

2 4 23

3 1 33

3 2 33

3 3 34

3 4 36

3 5 38

3 6 39

END DATA.

COMPUTE LAGID=LAG(ID,1).

LIST.

 

DEFINE !LAGGY (!POS = !TOKENS(1)).

!IF (ID !EQ LAGID) !THEN

 COMPUTE !CONCAT('LAG1',!1) = LAG(!1,1).

EXE.

!IFEND.

!ENDDEFINE.

!LAGGY X.

LIST.

 

 

_____________________________________________________________

 Jason W. Beckstead, Ph.D.       

  Associate Professor/Quantitative Methodologist

  University of South Florida College of Nursing

  12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA

  Statistical Editor, International Journal of Nursing Studies

  phone: +1.813.974.7667  fax: +1.813.974.5418          

  personal website:  http://personal.health.usf.edu/jbeckste/

  International Journal of Nursing Studies  http://www.elsevier.com/ijns

 

 

 

Reply | Threaded
Open this post in threaded view
|

Re: conditional compute within a macro

Jim Marks

Jason:

The IF structure does not need the !IF macro function-- regular commands can function within a macro.

Does this produce what you want?:

DEFINE !LAGGY (!POS = !TOKENS(1)).
DO IF (ID EQ LAGID).
COMPUTE !CONCAT('LAG1',!1) = LAG(!1,1).
END IF.
EXE.
!ENDDEFINE.

!LAGGY X.

LIST.


Jim Marks
Sr Market Research Manager
National Market Research
Kaiser Foundation Health Plan of the Mid-Atlantic States, Inc.
2101 E. Jefferson St.
Rockville, MD 20852
Phone: (301) 816-6822
Cell Phone: (605) 929-3262

NOTICE TO RECIPIENT:  If you are not the intended recipient of this e-mail, you are prohibited from sharing, copying, or otherwise using or disclosing its contents.  If you have received this e-mail in error, please notify the sender immediately by reply e-mail and permanently delete this e-mail and any attachments without reading, forwarding or saving them.  Thank you.




From:        "Beckstead, Jason" <[hidden email]>
To:        [hidden email]
Date:        03/22/2012 08:28 AM
Subject:        conditional compute within a macro
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Listers,
I am trying to compute a lagged variable within a macro using CONDITIONAL, !IF !THEN, but having no luck.
can anyone offer assistance? Below is simplified data set/structure and current macro attempt.
 
Thanks.
 
DATA LIST FREE/ ID DAY X.
BEGIN DATA.
1 1 10
1 2 15
1 3 20
1 4 25
1 5 30
2 1 20
2 2 21
2 3 22
2 4 23
3 1 33
3 2 33
3 3 34
3 4 36
3 5 38
3 6 39
END DATA.
COMPUTE LAGID=LAG(ID,1).
LIST.
 
DEFINE !LAGGY (!POS = !TOKENS(1)).
!IF (ID !EQ LAGID) !THEN
 COMPUTE !CONCAT('LAG1',!1) = LAG(!1,1).
EXE.
!IFEND.
!ENDDEFINE.
!LAGGY X.
LIST.
 
 
_____________________________________________________________
 Jason W. Beckstead, Ph.D.        
  Associate Professor/Quantitative Methodologist
  University of South Florida College of Nursing
  12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA
  Statistical Editor, International Journal of Nursing Studies
  phone: +1.813.974.7667  fax: +1.813.974.5418          
  personal website:  http://personal.health.usf.edu/jbeckste/
  International Journal of Nursing Studies  http://www.elsevier.com/ijns
 
 
 
Reply | Threaded
Open this post in threaded view
|

Re: conditional compute within a macro

David Marso
Administrator
In reply to this post by Beckstead, Jason
Please indicate what your expectations are and the current state contrary to such expectations!
Hard to help fix something when one has NO idea what your intentions are and the current outcome.
I have decided to refrain from utilizing paranormal means for addressing questions on this forum.
--
Beckstead, Jason wrote
Listers,
I am trying to compute a lagged variable within a macro using CONDITIONAL, !IF !THEN, but having no luck.
can anyone offer assistance? Below is simplified data set/structure and current macro attempt.

Thanks.

DATA LIST FREE/ ID DAY X.
BEGIN DATA.
1 1 10
1 2 15
1 3 20
1 4 25
1 5 30
2 1 20
2 2 21
2 3 22
2 4 23
3 1 33
3 2 33
3 3 34
3 4 36
3 5 38
3 6 39
END DATA.
COMPUTE LAGID=LAG(ID,1).
LIST.

DEFINE !LAGGY (!POS = !TOKENS(1)).
!IF (ID !EQ LAGID) !THEN
 COMPUTE !CONCAT('LAG1',!1) = LAG(!1,1).
EXE.
!IFEND.
!ENDDEFINE.
!LAGGY X.
LIST.


_____________________________________________________________
 Jason W. Beckstead, Ph.D.
  Associate Professor/Quantitative Methodologist
  University of South Florida College of Nursing
  12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA
  Statistical Editor, International Journal of Nursing Studies
  phone: +1.813.974.7667  fax: +1.813.974.5418
  personal website:  http://personal.health.usf.edu/jbeckste/
  International Journal of Nursing Studies  http://www.elsevier.com/ijns
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: conditional compute within a macro

Jon K Peck
In reply to this post by Beckstead, Jason
See below.

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




From:        "Beckstead, Jason" <[hidden email]>
To:        [hidden email]
Date:        03/22/2012 06:33 AM
Subject:        [SPSSX-L] conditional compute within a macro
Sent by:        "SPSSX(r) Discussion" <[hidden email]>




Listers,
I am trying to compute a lagged variable within a macro using CONDITIONAL, !IF !THEN, but having no luck.
can anyone offer assistance? Below is simplified data set/structure and current macro attempt.
 
Thanks.
>>>The macro facility expands the entire macro before any of the code in it is run.  So when you use !IF, that condition will  be evaluated once only, not on every case.  With this particular macro, the condition is not true, therefore the body of the macro will also be empty.  One positive benefit of this is that the superfluous and resource wasting EXE command will never generated :-)

You could rewrite the macro definition as
DEFINE !LAGGY (!POS = !TOKENS(1)).
IF (ID EQ LAGID)   !CONCAT('LAG1',!1) = LAG(!1,1).
!ENDDEFINE.

which uses a transformation system IF rather than a macro IF.
Since this expands just to this,
IF (ID EQ LAGID)  LAG1X = LAG( X ,1).
the macro doesn't do much more you, but maybe the true context is more complicated.

Note, though, that constructs like this can be better handled by SHIFT VALUES.
If you turn on SPLIT FILES based on the id,
then this command does what you want.
 
SHIFT VALUES VARIABLE=ID RESULT=lag1x LAG=1.

That is, the SHIFT VALUES lag does not cross split groups.

HTH,
Jon Peck



DATA LIST FREE/ ID DAY X.
BEGIN DATA.
1 1 10
1 2 15
1 3 20
1 4 25
1 5 30
2 1 20
2 2 21
2 3 22
2 4 23
3 1 33
3 2 33
3 3 34
3 4 36
3 5 38
3 6 39
END DATA.
COMPUTE LAGID=LAG(ID,1).
LIST.
 
DEFINE !LAGGY (!POS = !TOKENS(1)).
!IF (ID !EQ LAGID) !THEN
 COMPUTE !CONCAT('LAG1',!1) = LAG(!1,1).
EXE.
!IFEND.
!ENDDEFINE.
!LAGGY X.
LIST.
 
 
_____________________________________________________________
 Jason W. Beckstead, Ph.D.        
  Associate Professor/Quantitative Methodologist
  University of South Florida College of Nursing
  12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA
  Statistical Editor, International Journal of Nursing Studies
  phone: +1.813.974.7667  fax: +1.813.974.5418          
  personal website:  http://personal.health.usf.edu/jbeckste/
  International Journal of Nursing Studies  http://www.elsevier.com/ijns
 
 
 
Reply | Threaded
Open this post in threaded view
|

Re: conditional compute within a macro

David Marso
Administrator
In reply to this post by Jim Marks
In fact, the !IF simply compares the literal strings ID, LAGID and will *ALWAYS* be false.
ie. MACRO merely deals with *STRINGS* and has no clue re the values of ID or LAGID.
Jim's code can be further simplified since there is only a single expression involved in the conditional.
DEFINE !LAGGY (!POS = !TOKENS(1)).
IF (ID EQ LAGID)  !CONCAT('LAG1',!1) = LAG(!1,1).
!ENDDEFINE.
I have omitted the EXE in case there are other transformations you wish to perform prior to a data pass.
Note this will simply produce the syntax.
IF (ID EQ LAGID) LAG1X=LAG(X,1).
I'm really not sure that a macro is called for to build such simple syntax ;-)
---
Jim Marks wrote
Jason:

The IF structure does not need the !IF macro function-- regular commands
can function within a macro.

Does this produce what you want?:

DEFINE !LAGGY (!POS = !TOKENS(1)).
DO IF (ID EQ LAGID).
COMPUTE !CONCAT('LAG1',!1) = LAG(!1,1).
END IF.
EXE.
!ENDDEFINE.

!LAGGY X.

LIST.


Jim Marks
Sr Market Research Manager
National Market Research
Kaiser Foundation Health Plan of the Mid-Atlantic States, Inc.
2101 E. Jefferson St.
Rockville, MD 20852
Phone: (301) 816-6822
Cell Phone: (605) 929-3262

NOTICE TO RECIPIENT:  If you are not the intended recipient of this
e-mail, you are prohibited from sharing, copying, or otherwise using or
disclosing its contents.  If you have received this e-mail in error,
please notify the sender immediately by reply e-mail and permanently
delete this e-mail and any attachments without reading, forwarding or
saving them.  Thank you.




From:   "Beckstead, Jason" <[hidden email]>
To:     [hidden email]
Date:   03/22/2012 08:28 AM
Subject:        conditional compute within a macro
Sent by:        "SPSSX(r) Discussion" <[hidden email]>



Listers,
I am trying to compute a lagged variable within a macro using CONDITIONAL,
!IF !THEN, but having no luck.
can anyone offer assistance? Below is simplified data set/structure and
current macro attempt.

Thanks.

DATA LIST FREE/ ID DAY X.
BEGIN DATA.
1 1 10
1 2 15
1 3 20
1 4 25
1 5 30
2 1 20
2 2 21
2 3 22
2 4 23
3 1 33
3 2 33
3 3 34
3 4 36
3 5 38
3 6 39
END DATA.
COMPUTE LAGID=LAG(ID,1).
LIST.

DEFINE !LAGGY (!POS = !TOKENS(1)).
!IF (ID !EQ LAGID) !THEN
 COMPUTE !CONCAT('LAG1',!1) = LAG(!1,1).
EXE.
!IFEND.
!ENDDEFINE.
!LAGGY X.
LIST.


_____________________________________________________________
 Jason W. Beckstead, Ph.D.
  Associate Professor/Quantitative Methodologist
  University of South Florida College of Nursing
  12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA
  Statistical Editor, International Journal of Nursing Studies
  phone: +1.813.974.7667  fax: +1.813.974.5418
  personal website:  http://personal.health.usf.edu/jbeckste/
  International Journal of Nursing Studies  http://www.elsevier.com/ijns
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: conditional compute within a macro

Beckstead, Jason
In reply to this post by Beckstead, Jason

Thanks to all you guys who offered help with the IF vs !IF. I tried to just post the part I was having trouble with, but in the interest of completeness, here is the big picture.

 

I have a colleague who has longitudinal data on women beginning with the birth of a child. Within the first week postpartum, and then at 1 month intervals for 12 months she has a nurse do home visits and collect blood samples so that assays of various cytokines (IL10, IL6, TNF-alpha, etc.), about 30 or so can be done. Some of the women develop post-partum thryoiditis (PPT) and this is documented on a particular month-visit. She wants to look for elevations in these cytokines that precede (by one or two months) testing positive for PPT. I have data in tall file (up to 12 records per subject). I created a binary indicator (PPTCHG) that is 1 on the visit where PPT positive is documented, otherwise indicator is 0, to identify when PPT status changed. I wanted to give here a simple macro that she could use to conduct some exploratory (preliminary) analyses.

 

Just to let you see what I was doing I paste code below., Again, thanks for your help.

 

*---a macro for creating lag1 & lag2 versions of variables and then testing

 their relationship to the change in ppt status variable------------------------.

DEFINE !LAGGY (!POS = !TOKENS(1)).

DO IF (ID EQ LAG(ID)).

+COMPUTE !CONCAT('LAG1',!1) = LAG(!1,1).

+COMPUTE !CONCAT('LAG2',!1) = LAG(!1,2).

END IF.

REGRESSION/DES=CORR/STATS=COEFF ANOVA TOL/CRITERIA=PIN(.15) POUT(.20)

 /DEP=PPTCHG/FORWARD !1 !CONCAT('LAG1',!1) !CONCAT('LAG2',!1).

LOGISTIC REGRESSION PPTCHG/METHOD=FSTEP !1 !CONCAT('LAG1',!1) !CONCAT('LAG2',!1)

 /PIN(.15) POUT(.2).

!ENDDEFINE.

*to run:

 simply type "!LAGGY" followed by the name of the variable you want to analyze.

*------------------------------------------------------------------------------.

 

 

_____________________________________________________________

 Jason W. Beckstead, Ph.D.       

  Associate Professor/Quantitative Methodologist

  University of South Florida College of Nursing

  12901 Bruce B. Downs Blvd., MDC22, Tampa, FL 33612, USA

  Statistical Editor, International Journal of Nursing Studies

  phone: +1.813.974.7667  fax: +1.813.974.5418          

  personal website:  http://personal.health.usf.edu/jbeckste/

  International Journal of Nursing Studies  http://www.elsevier.com/ijns

 

 

 

Reply | Threaded
Open this post in threaded view
|

Automatic reply: Re: conditional compute within a macro

dtaylor_elca

I am out of the office until April 2.

Please contact [hidden email] if you need something before then.

Thank you.

Reply | Threaded
Open this post in threaded view
|

Re: conditional compute within a macro

David Marso
Administrator
In reply to this post by Beckstead, Jason
You might want to look at the CCF procedure
---
CCF
Overview
CCF  displays and plots the cross-correlation functions of two or more time series. You can
also display and plot the cross-correlations of transformed series by requesting natural log and
differencing transformations within the procedure.
Options
Modifying the Series. You can request a natural log transformation of the series using the LN
subcommand and seasonal and nonseasonal differencing to any degree using the SDIFF and
DIFF subcommands. With seasonal differencing, you can also specify the periodicity on the
PERIOD subcommand.
Statistical Display. You can control which series are paired by using the keyword WITH. You can
specify the range of lags for which you want values displayed and plotted with the MXCROSS
subcommand, overriding the maximum specified on TSET. You can also display and plot values at
periodic lags only using the SEASONAL subcommand.
.............
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?"