Announcement

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

  • Titles for graphs in loops

    Hello everyone,

    I'm trying to save some graphs, and the code is working for saving them, but I can't seem to title them anything useful.

    Here is the code:

    levelsof Fullid, local(cpm)
    foreach i in `cpm'{
    twoway connected mhos cpn if Fullid==`i', title("`i'")
    qui: graph export "$Raw Data`i'.pdf", replace as(pdf)
    }

    My problem is that I'd like to use the values of Fullid as the title, but because I'm using levelsof for the loop I just end up with each graph being titled 1, 2, 3 (etc. for each level of Fullid). Does anyone have a work around for this?

  • #2
    The local `i' in your command should contain the values of the variable Fullid. My first guess is that the variable Fullid contain the values 1,2,..,.k, but that it value labels attached to them that make more sense to you. You can check that by comparing the output:

    Code:
    list Fullid in 1/10
    list Fullid in 1/10, nolabel
    Can you run that code and tell us whether they gave you the same or different output?
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      One separate file for each identifier has to make me curious. How are you going to use the results?

      Comment


      • #4
        Maarten,

        I ran the code you mentioned and this is what I ended up with:

        . list Fullid in 1/10

        +------------------------------+
        | Fullid |
        |------------------------------|
        1. | Aeronca-Middletown-*PT-19-23 |
        2. | Aeronca-Middletown-*PT-19-23 |
        3. | Aeronca-Middletown-*PT-19-23 |
        4. | Aeronca-Middletown-*PT-19-23 |
        5. | Aeronca-Middletown-*PT-19-23 |
        |------------------------------|
        6. | Aeronca-Middletown-*PT-19-23 |
        7. | Aeronca-Middletown-*PT-19-23 |
        8. | Aeronca-Middletown-*PT-19-23 |
        9. | Aeronca-Middletown-*PT-19-23 |
        10. | Aeronca-Middletown-*PT-19-23 |
        +------------------------------+

        . list Fullid in 1/10, nolabel

        +--------+
        | Fullid |
        |--------|
        1. | 1 |
        2. | 1 |
        3. | 1 |
        4. | 1 |
        5. | 1 |
        |--------|
        6. | 1 |
        7. | 1 |
        8. | 1 |
        9. | 1 |
        10. | 1 |
        +--------+

        (It's a panel dataset so there are a bunch of entries, not just Aeronca-Middletown-*PT-19-23—1). So, what you're saying is that I need to run the loop so that the value label is attached rather than the variable?

        Nick,

        I'm comparing a series of companies (broken down into separate plants and broken down further by separate products) over 60 months. My plan is to graph different variables from each of these company/plant/product combinations and compare them between other company/plant/product combinations. Is assigning one separate identifier to each one not an efficient thing to do?

        Comment


        • #5
          Hi all,

          Maarten, thanks for the tip about the value labels. I've got the code working now:

          Code:
          levelsof Fullid, local(cpm)
          
          foreach i in `cpm'{
          local v1: label (Fullid) `i'
          twoway connected mhos cpn if Fullid==`i', title("`v1'")
          qui: graph export "$Raw Data `v1'.pdf", replace as(pdf)
          }

          I dug around through other statalist questions about this, and figured out that I needed to extract the value label and store it in a local within the loop. One thing that I don't understand is why the line needed to read:

          Code:
          local v1: label (Fullid) `i'
          Rather than:

          Code:
          local v1: label (`cpm') `i'
          If anyone can explain why the former worked and later didn't I would be appreciative. I'm trying to read as much as is possible to really understand Stata, but I'm still very much a beginner.

          Comment


          • #6
            cpm contains all your levels; you want to use the variable name there, not a list of its distinct values.

            Comment


            • #7
              Ah, I see. Thanks very much Nick!

              Comment

              Working...
              X