Announcement

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

  • Compound double quotes for single quotes

    Dear Statalisters,

    I'm wondering if there is a possibility like compound double quotes for single quotes used in locals. Here is what I am doing in a pretty crude example:

    Code:
    loc text=""
    forvalues i=1(1)2 {
          loc t1="var_`i'"
          loc text="`text' `t1'[`r']"
    }
    I would like to get this:
    loc text= var_1[`r'] var_2[`r']

    So that I can use it in another loop over r.

    But no matter what I try, it doesn't seem to be possible to write `r' into a new local without Stata directly replacing it by the not-existing local r.

    Thanks a lot for your help,
    Anna

  • #2
    You are probably looking for nested loops:

    Code:
    forvalues r= 4/5 {
        forvalues i = 1/2 {
           di var_`i'[`r']
       }
    }
    Moreover, it looks like you want to loop over observations. This is almost always a bad idea in Stata. Can you tell us more about what you really want to do?
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks for your answer! But that's not really what I'm looking for. I don't want to display this, but I want to use exactly "loc text= var_1[`r'] var_2[`r']" as another local.
      Maybe there is a better solution for my problem. I try to break down, what I want to do.
      I am generating a specific table from my data which I store in variables in a first step. So in my data I have something like this (a part of it):

      Lab n_g grp_1 grp_2
      D-Center 1023 (37%) 0.1 0.5
      Betablockers 46 (2%) 0.4 0.7
      First Episode 3 (0%) 0.3 0.9

      I need to do some further steps with these data, but I also want to display this as a table in my Stata Window. But the problem is that I don't know how many groups I have (it is supposed to be a universal ado-file).

      Normally, I would do something like this (If the number of groups would be known):

      Code:
      program test    
          
      loc pos0=17
      loc pos1=`pos0'+6
      loc pos2=`pos1'+15
      loc pos3=`pos2'+10   
          
      di as text %16s abbrev("Variable",16) " {c |}" _col(`pos1') "N(%)" _col(`pos2') "_grp_1" _col(`pos3') "_grp_2"
      di as text "{hline `pos0'}{c +}{hline `pos3'}"            
                 
      forvalues r=1/3 {
            di as text %16s abbrev(Lab[`r'],16) " {c |}" ///
                           _col(`pos1') n_g[`r']    ///
                           _col(`pos2') grp_1[`r']  _col(`pos3') grp_2[`r']
              }
              
      end
      So to deal with the problem, that I don't know how many groups I have, I wanted to get the part "_col(`pos2') grp_1[`r'] _col(`pos3') grp_2[`r']" out as a local from a foregoing loop, like this:

      Code:
      loc text=""
      loc pos=`pos2'
      forvalues i=1(1)n_grp {
           loc pos_grp`i'=`pos'+10
           loc pos=`pos_grp`i''
           loc text="`text' _col(`pos_grp`i'') _grp_`i'[`r']'"
           }
      If this would work, I could simply replace "text" within the display statement.

      Code:
      forvalues r=1/3 {
            di as text %16s abbrev(Lab[`r'],16) " {c |}" ///
                           _col(`pos1') n_g[`r']    ///
                           `text'
              }
      But maybe there's a better way to deal with this.

      Comment


      • #4
        So in your case the number of groups is the number of observations in the dataset? You can get the number of observations with _N or c(N), see help _variables and help creturn respectively. Here is one sketch of how to use that:

        Code:
        forvalues r = 1 \ `c(N)' {
            do stuff
        }
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          No, that's not that problem. I am computing values for a unknown number of groups which I write then into the respective variables grp_1 grp_2 ... grp_n. So the number of variables which I want do display in a stata output window table is not known.

          Comment


          • #6
            The basics would remain the same: first find out the number of groups, and than loop rather than the other way around. Since you are not telling us where those groups come from, there is not much more for us to say.
            ---------------------------------
            Maarten L. Buis
            University of Konstanz
            Department of history and sociology
            box 40
            78457 Konstanz
            Germany
            http://www.maartenbuis.nl
            ---------------------------------

            Comment

            Working...
            X