Announcement

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

  • Assign corresponding elements from two lists

    Dear Statlist,

    I have a number of small datasets with the same structure and I would like to rename a variable (with a common name across datasets) in each dataset with a different name.

    Say I have datasets (data1 data3 data6 ...) where I want to rename variable y (same name in all datasets) with (aaa bbb ccc ...) where "aaa" should be assigned to file "data1", "bbb" to "data2", and so on.

    If filename numbers were sequential I would write:

    Code:
    local newvarlist aaa bbb ccc
    local i=1
    foreach nv of local newvarlist {
        use ".../data`i'.dta", clear
        local ++i
        rename `y' `nv'
        save ".../`nv'.dta", replace
        }
    But as I am dealing with numerical suffixes in filenames that do not follow any logical sequence looping over a number isn't a solution and I wonder if you know any way to call the jth element of a local list within the jth iteration of a foreach loop.

    In more general terms, I would like to assign the ith element from a list of files to the corresponding ith element in a list of variables.

    Thank you,
    Andre

  • #2
    I don't follow exactly what you are trying to do. But the way to refer to the j'th element of a local macro is `:word `j' of `my_local_macro''.

    E.g.

    Code:
    local mylist abc def foo bar qwerty
    forvalues j = 1/5 {
        display `"`:word `j' of `mylist''"'
    }

    Comment


    • #3
      Dear Clyde,

      Notwithstanding, your answer was very helpful to me. With the
      Code:
      `"`:word `j' of `mylist''"'
      i am able to store the jth element of different lists and follow the same order while looping over lists.

      Thank you,
      Andre

      Code:
      local mainvar abc def foo bar qwerty
      local fileno 2 7 12 17 19
      forvalues l = 1/5 {
          local m = `"`:word `l' of `mainvar''"' 
          local n = `"`:word `l' of `fileno''"'
          
          use ".../data_`n'.dta", clear
          rename y `m'    
      
          save ".../`m'.dta", replace
      }
      *

      Comment


      • #4
        See also the survey of loops in parallel in Stata Journal 21(4) 2021.

        Comment


        • #5
          Here's a link to the column just cited. https://journals.sagepub.com/doi/pdf...6867X211063415


          Here is one of several ways to do it. This destroys the local macro mainvar and it that is not wanted, then you need some slightly different code.

          Code:
          local mainvar abc def foo bar qwerty
          local fileno 2 7 12 17 19
          
          foreach f of local fileno { 
              gettoken m mainvar : mainvar 
              
              use ".../data_`f'.dta", clear
              rename y `m'    
          
              save ".../`m'.dta", replace
          }

          Comment


          • #6
            Dear Nick,

            Thank you for your advice, valuable as always. -gettoken- and -tokenize- are indeed alternatives to the syntax above.

            Andre

            Comment

            Working...
            X