Announcement

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

  • Summary statistics through a loop

    Hi all,

    I have a large data set that I want to generate summary statistics for. I have a range of variables that I want to have generated but I can't seem to get for loops to work. Essentially, I want to have a table that generates 2 columns (1 for the Control Group and the other for the Intervention group) that has all the variables I want as new rows.

    Below is an excerpt of my code with just a few of the variables I want to generate (age, race, sex, etc.). All variables are categorical variables. The code below yields the error r(109) "type mismatch" and I can't seem to figure out why. Any help will be much appreciated.


    Code:
    local sumstats racecat1n age stkcat1n sscat2n sexn hisstktian
    local armcd CON INV
    local armn 1 2
    #delimit;
    forvalues r=1/2{;
    eststo `=word("`armcd'",`r')': estpost tabstat `sumstats' if armcd==`=word("`armn'",`r')', stat(mean sd) columns(stat) listwise;
    };
    esttab `armcd'  using "$output/mcd_summarystats.tex", replace  label
    title ("Table 1: Baseline Characteristics by Arm for the Claims ITT Populations,")
        addnotes("The means are reported with the standard deviations in parenthesis")
        cells(mean(pattern(1 1) fmt(3)) sd(par pattern(1 1)fmt(2)) b(star pattern(0 0 1) fmt(2)))
        mtitles( "UC" "INV")
        varlabel(
        racecat1n "Race Category"
        stkcat1n "Stroke Diagnosis"
        age "Age (years)"
        sscat2n "NIHSS Category"
        sexn "Sex"
        hisstktian "History of Ssstroke or TIA") ;
    #delimit cr

  • #2
    It is hard to debug this without any kind of data example. At the same time we appreciate that can be hard to supply if the dataset is large and complicated.

    I will try. My eye was drawn to this fragment as likely to evoke that error message.

    Code:
     
     if armcd==`=word("`armn'",`r')'
    If
    Code:
     armcd
    is a string variable you need

    Code:
     
     if armcd=="`=word("`armn'",`r')'" 
    as then you're testing for equality with a literal string whereas if armcd is the name of the local macro you need
    Code:
    if "`armcd'" =="`=word("`armn'",`r')'"


    as then you're comparing two literal strings.

    Possible morals large and small:

    1. This is a loop over two values. I wonder how much time has been spent trying to debug this when the raw code is just twice as long.. On the other hand, one never learns loops without trying them.

    2. Horrible bugs can arise if the same name is used for a variable and a macro. (This may not be happening here.)

    Comment

    Working...
    X