Announcement

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

  • Nested loop over variable name

    In my dataset I have 4 "characteristics" (like income, type of degree etc.) for 3 individuals. In the datafile, the variables are numbered consecutively, say var1-var12. So var1 is the first characteristic for the first individual, var2 is the second characteristic for the first individual, and var10 is the second characteristic for the third individual.

    I want to assign labels to a subset of the characteristics (say the 1st, 3rd and 4th) for all the individuals. I therefore defined 3 labels, lbl_var1, lbl_var3 and lbl_var4. Now I tried to set up a loop for this purpose, but it didn't work out. Here is what I tried at first:

    local itemlist "1 3 4"
    foreach i of local itemlist {
    foreach j of numlist 0(1)2 {
    label values var(`i' + `j'*4) lbl_var`i'
    }
    }

    This apparently doesn't work out, because Stata then reads only the part inside the brackets (`i' + `j'*4) as variable name. I get the error "1 invalid name".

    I then tried a workaround for this problem. In a first step I wanted to set up a local itemlist that contains all the numbers of the variables to be labeled, and then use this in a loop to label the variables. This is what I did:

    local itemlist "1 3 4"
    foreach j of numlist 0(1)2 {
    foreach i of local itemlist {
    local itemlist2 `itemlist2' `i' + `j' * 4
    }
    }

    foreach i of local itemlist {
    foreach j of local itemlist2 {
    label values var`j' lbl_var`i'
    }
    }

    But this didn't work as well. The setup of the local doesn't seem to work, I get the error "variable var not found"


    Can someone tell me, how I can set up a nested loop over variable names as in this example?


    Note: The number of variables I have is actually much larger. I kept the numbers small in this example to keep things simple.

  • #2
    I think what you want is:
    Code:
    local itemlist "1 3 4"
    foreach i of local itemlist {
        foreach j of numlist 0(1)2 {
             label values var`=`i' + `j'*4'  "lbl_var`i'"
        }
    }
    That said, this data layout is likely to prove difficult or impossible to work with once you get down to serious analysis in Stata. The majority of Stata data management and analysis commands are optimized for use with data in long layout. So you may want to consider converting it along these lines:

    Code:
    forvalues i = 1/12 {
        local person = ceil(`i'/4)
        local attribute = mod(`i', 4) + 1
        rename var`i' var`attribute'_`person'
    }
    reshape long var, i(obs_no) j(_j) string
    split _j, parse("_") destring gen(_j)
    rename _j1 attribute
    rename _j2 person
    drop _j
    You will probably find it easier to work with the data this way.

    Comment


    • #3
      Thank you very much!

      Yes, you are right about the data format, I am also used to working with data in long format. However, I need to work with this dataset at work and am not the only one using it, so I can't just change it. But it will be mostly about descriptive statistics, so it probably (or hopefully) doesn't cause too many problems.

      Comment


      • #4
        Dear Clyde Schechter,

        I am encountering a similar problem, and although I searched the web extensively, I did not find a solution. Probably I am using the wrong words in m search...
        I want to combine a global with a local in a loop, as in this example:

        Code:
        global Masterpath "D:\Task"
        local filelist "file1 file2 file3"
        
        foreach file of filelist {
        import excel "$Masterpath\`file'", clear
        
        [...]
        }
        However, the combination of the local with the global macro will not work in this example. I have already tried many different variations, but I could not achieve a working solution...

        I would be glad if you (or anyone else that stumbles upon this post) could provide me with a solution.

        Kind regards
        Pascal Weber

        Comment


        • #5
          The problem is with the occurrence of the sequence \`. It is not what it appears to be. The Stata parser, upon seeing \` says "that ` is not the start of a reference to a local macro, it's a literal ` character. So it does not expand `file' to file1 file2 or file3 and instead it tells import excel to look for a file called D:\TASK`file' (where `file' is the literal sequence of characters ` f i l e ', not file1, file2, or file3.)

          The best way to get around this is to not use \ as the path separator. Use / instead. Windows does not recognize the / character as a path separator, but when Stata passes paths and filenames to Windows, it will make the substitution for you. So:

          Code:
          local Masterpath D:/task
          local filelist file1 file2 file3
          foreach file of filelist {
              import excel "`Masterpath'/`file'", clear
          }
          Notes:
          1. Because the use of global macros is a dangerous practice that should be avoided whenever any alternative exists, I have here replaced it with a local macro.
          2. I have removed the quotes from the definition of the local macro filelist. At best they are unnecessary, and at worst they might be misunderstood as meaning that "file1 file2 file3" as a whole is intended in the loop rather than iterating separately over file1, file2, and file3.

          As an aside, it is not a good idea to address a web post to a specific person. This is a Forum with a large membership. Particular members are not always logged in and reading. We all come and go. By addressing your question to one person, you discourage other people who could provide a helpful response sooner from answering.

          Comment


          • #6
            Thank you very much for your answer, again!

            These are explanations which will be very helpful in the future for me. Great that you also took the time to point to some further issues, like the use of globals and the using of quotes in setting up locals. I got used to using quotes in the set up of locals when you want to have longer items with spaces inbetween. Then they are quite handy when you use double quotes.

            As to your aside, I am aware that this qould be considered unusual, but it felt natural to me to expand this thread, as the topic was that much related. But I will take your advice and refrain from addressing specific people in the future.

            Comment

            Working...
            X