# conditional compute within a macro

8 messages
Open this post in threaded view
|

## conditional compute within a macro

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

## Re: conditional compute within a macro

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

## Re: conditional compute within a macro

Open this post in threaded view
|

## Re: conditional compute within a macro

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

## Re: conditional compute within a macro

Open this post in threaded view
|

## Re: conditional compute within a macro

 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