Announcement

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

  • How to display variable (string) value in matrix

    Hi,

    I am using a loop to collect summary statistics using the matrix function. For example, the columns are districtid districtname (string variable) and minimum_score. districtid is an arbitrary numerical id for each district.
    forval dst=1/3 {
    summarize score if districtid==`dst'
    local min_score = r(min)
    matrix stats[`dst',1] = `dst'
    matrix stats[`dst',2] = ????????????????
    matrix stats[`dst',3] = `min_score'
    display districtname if districtid==`dst'
    }
    I have no problem collecting the min_score statistics, but I have problems displaying the district name in the matrix. I can't define a local containing the district name using the display or tabulate function. Any ideas how I can get the matrix to store the districtname? I would prefer to display the districtname rather than the districtid because districtid is arbitrary to the dataset.

    Thank you.
    Best,
    Yi


  • #2
    This can be done with Mata, but in your case I would just use matrix rownames instead. The only limitation I see here is that the numerical district id will follow the district name.

    Comment


    • #3
      Thank you for the tip! I'll try Mata.

      Comment


      • #4
        You cannot have mixed datatypes in Mata matrixes, either.

        I'm not sure why you want the collection to be in a matrix—why not put it into a Stata dataset, instead? That way, you can take advantage of statsby:
        Code:
        statsby minimum_score = r(min), by(districtname) clear: summarize score, meanonly
        list , noobs abbreviate(20)
        As an alternative that requires a little coding, but that is pretty flexible, you could try something like the following. It shouldn't be too involved to adapt to your needs.
        Code:
        tempname fh
        tempfile tmpfil
        postfile `fh' int district_id str244 district_name double minimum_score using `tmpfil'
        forvalues dst = 1/3 {
            quietly levelsof districtname if districtid == `dst'
            local district_name `r(levels)'
            summarize score if districtid == `dst', meanonly
            post `fh' (`dst') ("`district_name'") (r(min))
        }
        postclose `fh'
        use `tmpfil', clear
        list  , noobs abbreviate(20)
        In both examples, I've assumed that your dataset has already been saved, but either way can easily be set up to save the summary statistics to a permanent Stata dataset file instead of calling up to replace your in-memory dataset.

        Comment

        Working...
        X