Announcement

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

  • Duplicate all the values within a local

    Hello all

    Imagine we have a local called years which when printed out/displayed looks like this:

    Code:
    1990 1991 1992 1993 1994
    I want to somehow create a new local, called (let's say) years_doubled, that looks as follows:
    Code:
    1990 1990 1991 1991 1992 1992 1993 1993 1994 1994
    Any ideas?

    The purpose behind this is that I am using putexcel and for each year I want to report an N and a mean score per year. I am going to use the desired local to make column names for my matrix.

    Regards,
    Bruce

    Code for replication:
    Code:
    local years 1990 1991 1992 1993 1994

  • #2
    Code:
    . local years 1990 1991 1992 1993 1994
    
    . local years_doubled `years' `years'
    
    . local years_doubled : list sort years_doubled
    
    . di "`years_doubled'"
    1990 1990 1991 1991 1992 1992 1993 1993 1994 1994
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thanks Maarten!

      This solutions works but only assuming the alphabetic sorting will give the correct arrangement.

      I have since thought up a different (messier) solution that preserves the order of the original. See below using a different example where the alphabetic sorting would be a problem if we wanted to preserve the original order (by size in this case):

      Code:
      local animals elephant dog cat mouse
      local i=wordcount("`animals'")
      di `i'
      local n=`i'*2
      forvalues i=1(2)`n' {
      di `i'
      local word: word `i' of `animals'
      di "`word' is word"
      local animals=subinstr("`animals'","`word'","`word' `word'",.)
      di "`animals'"
      }

      Comment


      • #4
        Here's another way to do it:

        Code:
        local animals elephant dog cat mouse
        
        foreach beast of local animals {
            local new `new' `beast' `beast'
        }
        
        di "`new'"
        
        elephant elephant dog dog cat cat mouse mouse
        Or start with

        Code:
        foreach beast in elephant dog cat mouse {

        Comment


        • #5
          Clever and neat, thanks Nick

          Comment


          • #6
            I want to duplicate local content in a given number. For example, the local foo stores one word "animals", and according to a given number, I have to let foo store five copies of "animals". How can I get that result? (And, I find a weird problem. I write the following code in Stata Do-file Editor, then I paste it here surrounded with . And I copy and paste it back to my Stata Do-file Editor, however, Stata report an error saying that local is not a valid command name r(199);.)

            Code:
            local foo animals
            display "`foo'"
            
            forvalues i=1(2)5 {
             local foo `foo' `foo'
             }
            display "`foo'"

            Comment


            • #7
              Code:
              . local foo "animals"
              
              . local bar : display _dup(5) "`foo'"
              
              . local bla : display _dup(5) "`foo' " // notice the space
              
              .
              . di "`bar'"
              animalsanimalsanimalsanimalsanimals
              
              . di "`bla'"
              animals animals animals animals animals
              ---------------------------------
              Maarten L. Buis
              University of Konstanz
              Department of history and sociology
              box 40
              78457 Konstanz
              Germany
              http://www.maartenbuis.nl
              ---------------------------------

              Comment


              • #8
                For some more technique:

                Code:
                . local foo = 5 * "animals"
                
                . di "`foo'"
                animalsanimalsanimalsanimalsanimals
                
                . local foo = 5 * "animals "
                
                . di "`foo'"
                animals animals animals animals animals 
                
                . di "|`foo'|"
                |animals animals animals animals animals |
                
                . di ("|" + trim("`foo'") + "|")
                |animals animals animals animals animals|

                Comment


                • #9
                  Thank you so much Maarten Buis and Nick Cox. I have never thought that local can be used in such a (simple) way. I have been tormented by quote and double quote in Stata. Fortunately, based on your code, I sucessfully multiple a variable by five times in a local. Thank you again!

                  Code:
                  . sysuse auto, clear
                  (1978 Automobile Data)
                  
                  . local prev foreign
                  
                  . local foo "`prev'"
                  
                  . display "`foo'"
                  foreign
                  
                  . levelsof rep78
                  1 2 3 4 5
                  
                  . local foo= `r(r)' * `"`prev' "'
                  
                  . display "`foo'"
                  foreign foreign foreign foreign foreign

                  Comment

                  Working...
                  X