Announcement

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

  • Combination of for loop and while loop

    Code:
    clear
    set more off
    local x = 1
    use "file1"
    local z = `x' + 1
    while `x' <=3 & `z' <=3 {
    
    joinby monthid using "file2"
    
    local x = `x' + 1
    local z = `z' + 1
    }
    
    local d = 0
    local e = `d' + 1
    
    
    foreach a in rental income {
        foreach b of numlist 1(1)4 {
            foreach c in m q  {
                gen callt_se_`c'_pctuavg`a'`b' = 0 
            }
        }
    }
    
    foreach a in rental income {
        foreach b of numlist 1(1)4 {
            foreach c in m q  {
                gen calla_se_`c'_pctuavg`a'`b' = 0 
            }
        }
    }
    
    foreach a in rental income {
        foreach b of numlist 1(1)4 {
            foreach c in m q  {
                local `e' = 1
                while `e' <= 3 {
                replace callt_se_`c'_pctuavg`a'`b' = callt_se_`c'_pctuavg`a'`b' + c_`e'_se_`c'_pctuavg`a'`b'
                local e = `e' + 1
                }
            }
        }
    }
    
    local d = 0
    local e = `d' + 1
    
    foreach a in rental income {
        foreach b of numlist 1(1)4 {
            foreach c in m q {
                while `e' <= 3 {
                replace calla_se_`c'_pctuavg`a'`b' = (callt_se_`c'_pctuavg`a'`b'/3)
                local e = `e' + 1
            }
        }
    }
    }
    I know the code might be hard to understand but I still hope I can get some help. The above piece of code illustrates the problem I am currently working with. From above code I have the main variables
    Code:
    c_`e'_se_`c'_pctuavg`a'`b'
    which in one way can be
    Code:
    c_1_se_q_pctuavgrental2
    for example but also
    Code:
    c_1_se_m_pctuavgincome4
    . c_1_ indicates city one and c_2_ indicates city two. What I want to do is create averages for every city, city 1 to 3 in this case (even though it are many more). For every variable included per city I want to add them and then create an average. The way it works now it only works for the following variables:
    Code:
    callt_se_m_pctuavghuurprijs1 calla_se_q_pctuavghuurprijs1
    . How can I get this code to also work for the other variables determined in the forloops?

    Thank you in advance!

  • #2
    Your post has been up for a while and nobody has responded. Please read the Forum FAQ for excellent advice on how to post questions in ways that maximize your chances of getting a timely and helpful response.

    The code you show contains syntax errors and clearly is not anything you have actually run. Moreover, some of it makes no sense or is, at best, convoluted. Your final question "how can I get this code to also work for the other variables" would, in any case, be difficult to answer because it is by no means clear from what you have shown just what you are actually trying to do.

    The best I can make of it is that this code is some kind of modification of code that you actually did use to create a bunch of variables, and now you want to average some of those variables, but who knows which ones. So my advice is to repost, using the -dataex- command to show an example of the dataset with those variables already created. Then make it very clear exactly which (sets of) variables you wish to average together.

    I also suggest you read the -foreach- and -forvalues- sections of the PDF documentation that comes installed with Stata. Although -while- is still a valid Stata command, and is sometimes truly necessary for looping, most Stata loops, and in particular the kinds of loop you show in your code that iterate over static lists, are better done with -foreach- or -forvalues-. Also, do read the section on -by-. Stata's programming language is designed to minimize the use of explicit looping. You may find that with better organization of your data the -by- prefix will do the job and you may need fewer loops, or even none, Also check out the -egen, mean()- and -egen, rowmean()- functions.

    Comment

    Working...
    X