Announcement

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

  • Extracting parts of string local for use in graph combine

    Hi there,
    I am trying to use words stored in a string local to send to graph combine in sets in a loop. I am having trouble extracting what I want and giving it to graph combine.

    The local stores names of graph files and is of the form (not actual text and simplified here):
    Code:
    local graphnames `" "this is the first name" "this is the second name" "this is the third name" "this is the fourth name" "this is the fifth name" [...etc...] "'
    There are 192 graph names in the local. I want to combine the graphs in groups of 16. I do not know how to write a loop that can do this.

    Any pointers?
    Will

  • #2
    A self-help pointer:
    Code:
    local graphnames `" "this is the first name" "this is the second name" "this is the third name" "this is the fourth name" "this is the fifth name" [...etc...] "'
    local g = 0
    foreach t of local graphnames {
       local ++g
       di `"Title `g': "`t'""'
    }
    Last edited by Dirk Enzmann; 15 Sep 2016, 08:06.

    Comment


    • #3
      Hi Dirk,
      I'm still not managing it, I'm afraid.

      I've been trying to use something like this:

      Code:
      local graphnames `" "weight kg" "ALT" "In care" "COPD SMP" [...etc...] "'
      forval x = 1/16 {
          local y : word `x' in `graphnames'
          local g `g' `y'
      }
      graph combine `g', altshrink
      I'm getting confused with all the ins and outs of quotes!

      Will




      Comment


      • #4
        "something like this" is not as helpful as it could be. We need to see the exact code you are using. Otherwise what you show may include irrelevant details and exclude relevant details.

        For example, I can see that the line

        Code:
        local y : word `x' in `graphnames'
        is wrong if only because the syntax is word # of string. Also,

        Code:
        local g `g' `y'
        will fail because you need to use double quotes when the name added contains spaces.

        But -- stepping back from the details -- this seems very confused. You already have a list of graph names. What the loop seems intended to do is take the names out of the list one by one and assemble the list all over again. But there is no need to do twice what you already did.

        I can see no reason why "something like"

        Code:
        local graphnames `" "weight kg" "ALT" "In care" "COPD SMP" "'
        graph combine `graphnames', altshrink
        should not work.


        FWIW, I use graph names frequently and always give them names without embedded spaces precisely because I don't want to have to cope with those spaces.

        Comment


        • #5
          Sorry for the 'local word' error, and the lack of exact code. I'm working between a secure environment and a networked computer, so can't easily refer or copy between the two.
          The full list of graphs contains 192 graphs. I just want to combine them into sets of x (say 9, 12 or 16) for ease of looking at them.

          The graph names are due to the names being based on strings contained in a column of the dataset. I previously looped over the values in that column to create the graphs. I could strip out the spaces in the names, which would make it easier but make the graph file names illegible where I am saving them on the file system.

          Will



          Comment


          • #6
            I understand some of the difficulties. but already you have advice that applies:

            If your dataset is confidential, then provide a fake example instead.
            (FAQ #12)

            If 192 names are given in a variable (you say "column", but I guess you mean variable, not column; in Stata only matrices have columns), then you could go

            Code:
            gen id = _n
            gen group = ceil(id/12)
            
            forval g = 1/16 {
                   levelsof id if group == `g'
                   ...
            }
            but in the absence of almost any specific detail I can't go much further.




            Comment


            • #7
              Thanks Nick. I realise it's a bit odd. I just thought I would be able to do it relatively easily, and have got stumped.

              The dataset contains four variables: componentname, componentdate, componentvalue, componentid
              componentname contains strings (which are names of 192 different medical tests and conditions, for many different people at many different time points)
              componentdate contains "yyyy-mm-dd" strings of when the test or condition was made or identified
              componentvalue contains a float value recording the test outcome or a binary value recording the condition being present
              componentid is an ID number for the componentname

              I have encoded the "yyyy-mm-dd" strings contained in the componentdate to give an encoded yyyymm, named monthcode.

              I am then using the following code to create histograms to see how often each test/condition occurs in the dataset over time for each of the componentname values:

              Code:
              local componentnames `" "ALT" "Weight kg" "Diabetes" "COPD SMP" "On Admission Avoidance Care" [...etc...] "'
              
              foreach x of local componentnames {
                  hist monthcode if componentname=="`x'", disc freq saving("`x'", replace) title("`x'")
              }
              I then wanted to programmatically combine the resulting graphs in groups for presentation.

              Will





              Comment

              Working...
              X