Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop or Macro for three variable lists?

    I'm trying to determine the best way to run the same code multiple times through different variables.

    I have 3 variables that I want to work with (in a variable list type situation).

    yvar which my y-variable
    tvar which is a number associated with my y-scale
    nvar which is a string and is the title of the graph on the output.


    My code looks like this:

    Code:
    anova yvar Sex_AA2 Pre_LTCH Sex_AA2#Pre_LTCH / ///
        unique_ID|Pre_LTCH#Sex_AA2 ///
        data_point ///
        data_point#Sex_AA2 data_point#Pre_LTCH ///
        data_point#Sex_AA2#Pre_LTCH ///
        if Sex_AA2!=2, ///
        rep(data_point)
    
        margins, over(Pre_LTCH Sex_AA2 data_point)
        marginsplot, xdim(data_point) ylabel(0 tvar) ytick(0(1)tvar) graphregion(margin(5 20 10 5)) title("Predictive Margins for nvar") xtitle("Time point") ytitle("Adjusted Prediction (0-tvar)")
    examples would be:

    yvar DRS_Score CHESS_Score PAIN_Score
    tvar 13 5 3
    nvar Depression CHESS Pain (These are string values for the title)

    I want it to run through the first time using DRS_Score as the yvar, 13 as the tvar and Depression (a string) as the nvar, then again using CHESS_Score 5 CHESS, and again using PAIN_Score 3 Pain

    From what I've gathered from the manual and a few powerpoints I've found online I can't do this with loop because I can only assign one foreach value per loop (I don't know if this is actually correct it's what I've gathered from resources I've looked at).

    For local macros when I run it it seems to put the yvar as all my variables together instead of running it as a loop (using: local yvar DRS_Score CHESS_Score PAIN_Score I tried using $yvar in the code when using local and `yvar' when using loop).

    I also am not sure if what I want to do with nvar is even possible (to insert a variable name into a title).

    Any guidance is greatly appreciated
    Last edited by Elyse Cottrell; 19 Sep 2019, 12:31.

  • #2
    Code:
    local yvar DRS_Score CHESS_Score PAIN_Score
    local tvar 13 5 3
    local nvar Depression CHESS Pain 
    
    forval k = 1/3 { 
        local y : word `k' of `yvar'
        local t : word `k' of `tvar'
        local n : word `k' of `nvar'
    
        anova `y' Sex_AA2 Pre_LTCH Sex_AA2#Pre_LTCH / ///
        unique_ID|Pre_LTCH#Sex_AA2 ///
        data_point ///
        data_point#Sex_AA2 data_point#Pre_LTCH ///
        data_point#Sex_AA2#Pre_LTCH ///
        if Sex_AA2!=2, ///
        rep(data_point)
    
        margins, over(Pre_LTCH Sex_AA2 data_point)
        
        marginsplot, xdim(data_point) ylabel(0 `t') ytick(0(1)`t') ///
        graphregion(margin(5 20 10 5)) title("Predictive Margins for `n'") ///
        xtitle("Time point") ytitle("Adjusted Prediction (0-`t')")
    }

    Comment


    • #3
      That works beautifully, and I was able to do it with all the variables I need. Thank you so much I really appreciate your help.

      Originally posted by Nick Cox View Post
      Code:
      local yvar DRS_Score CHESS_Score PAIN_Score
      local tvar 13 5 3
      local nvar Depression CHESS Pain
      
      forval k = 1/3 {
      local y : word `k' of `yvar'
      local t : word `k' of `tvar'
      local n : word `k' of `nvar'
      
      anova `y' Sex_AA2 Pre_LTCH Sex_AA2#Pre_LTCH / ///
      unique_ID|Pre_LTCH#Sex_AA2 ///
      data_point ///
      data_point#Sex_AA2 data_point#Pre_LTCH ///
      data_point#Sex_AA2#Pre_LTCH ///
      if Sex_AA2!=2, ///
      rep(data_point)
      
      margins, over(Pre_LTCH Sex_AA2 data_point)
      
      marginsplot, xdim(data_point) ylabel(0 `t') ytick(0(1)`t') ///
      graphregion(margin(5 20 10 5)) title("Predictive Margins for `n'") ///
      xtitle("Time point") ytitle("Adjusted Prediction (0-`t')")
      }

      Comment

      Working...
      X