Announcement

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

  • Using the local macro in loop

    I am getting caught up in the code at the last point and was wanting some advice,

    I have a data of breast cancer
    Two Categorical Variables
    stage - Stage 1, 2, or 3
    type - Ductal (1), Medullary (2). Mucinous (3), Papillary (4)

    One Indicator Variable
    sex - Male (1), Female (2)

    One continous variable
    survtime

    I want to work out the average survival in stage by sex and type by sex,

    I have tried to code but get tripped up at the end,

    The following works and gives me overall means stratified by sex

    local vars stage type
    foreach x in local vars {
    forvalues i=1/2 {
    summarize survtime if sex==`i' , detail
    return list
    }
    }


    But when I try to split the groups by their individual categories I keep getting errors

    local vars stage type
    foreach x in local vars {
    forvalues i=1/2 {
    forvalues j=1/4{
    capture noisily summarize survtime if sex==`i' & `x”==`j', detail
    return list
    }
    }
    }

    `x” invalid name ???

    Thanks

  • #2
    Without reviewing your code to be sure it will do what you want once it works, in the line
    Code:
    capture noisily summarize survtime if sex==`i' & `x==`j', detail
    you have the wrong quotation mark following the x - you want
    Code:
    capture noisily summarize survtime if sex==`i' & `x'==`j', detail
    as you have after all your other local macro references.

    Comment


    • #3
      Hi William,

      That comes up with
      - local not found
      - vars not found

      The forvalues using j may not be the best technique?
      I'm trying some other options but not having much luck, essentially I need to create a formula to summarize a continuous variable stratified by two categorical variables,

      Thanks for your help,

      Comment


      • #4
        See

        Code:
        help foreach
        to see that you have a choice between

        Code:
        local vars stage type
        foreach x of local vars {
        and

        Code:
        foreach x in stage type {
        That said it sounds as if

        Code:
        bysort sex stage: summarize survtime, detail
        bysort sex type: summarize survtime, detail
        is more or less what you are trying to do.

        Last edited by Nick Cox; 20 May 2018, 23:39.

        Comment


        • #5
          Please note that the incorrect foreach command that Nick discusses in post #4 appears in your post #1 in both sets of code - including the set that you said works.

          It appears that the code you presented in post #1 is not the code that worked when you ran it.

          Please review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you have used to create each of your posts. Note especially sections 9-12 on how to best pose your question.

          Section 12.1 is particularly pertinent

          12.1 What to say about your commands and your problem

          Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!
          as is Section 12.3 on the use of code delimiters [CODE] and [/CODE] to present code and output copied from the results window.

          The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

          Comment

          Working...
          X