Announcement

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

  • Generating and labeling multiple variables with loop

    Hello!

    I am a beginner to STATA and am having issues trying to use loops. I am trying to generate 100 new variables (assigning them an arbitrary value), and label them all using a loop. However, using the code below, only one variable ends up being generated and labeled.

    Code:
    local i
    destring i, replace
    local x= 'i'+1
    foreach x of numlist 1/100{
    generate electricity_x= .
    label variable electricity_x "electricity x"
    }
    Am I missing a particular command or have I made an error in the code above? Any help would be greatly appreciated.

    Thanks!

    -Mika
    Last edited by Mika Inoue; 25 May 2018, 09:35.

  • #2
    "issues" doesn't report the error messages you received. It's on all fours with "didn't work"! Please go back and read FAQ Advice #12 (and also #18 on Stata not STATA).

    We can't see (an example of) your data, but commentary is possible.

    Code:
    local i
    That does nothing, including no harm. (Exception: if you had a previous local named i, it would delete it.)

    Code:
    destring i, replace
    Presumably what you want to do. No data example, so we can't comment.

    Code:
     local x= 'i'+1
    That should produce an error message. If you're implying that it worked, then you didn't copy it correctly to here.

    Code:
     local x= `i' + 1
    would work, and is just equivalent to

    Code:
    local x = 1
    It's irrelevant to what follows as the loop uses its own local.

    Code:
    foreach x of numlist 1/100 {    
       generate electricity_x= .    
       label variable electricity_x "electricity x"
    }
    That should do one thing, create a variable electricity_x that is all missing, and then fail because second time around the loop you try to create the same variable again. But it's easy to guess that you want

    Code:
    foreach x of numlist 1/100 {
        generate electricity_`x' = .
        label variable electricity_`x' "electricity `x'"
    }
    except that you are going to ask for something more interesting and useful than 100 variables all containing missing..

    I think most experienced Stata programmers would reach for forval here which gives shorter, simpler and faster code.

    Code:
    forval x = 1/100 {
        generate electricity_`x' = .
        label variable electricity_`x' "electricity `x'"
    }

    At this point, it's not at all clear what the destring command is doing that is connected to the rest of the code and hence why you mention it.



    Comment


    • #3
      Dear Mr. Cox,

      Thank you for your help! I'm sorry for being vague with my post, I'll try to avoid that in future posts. I actually just wanted to generate the 100 variables with them all containing missing... so thank you for your help with the forval as that was the only command I really needed.

      -Mika

      Comment


      • #4
        Code:
        gen emp_totMale= emp  if code==11| code==13| code==15| code==17| code==19| code==21 
        gen WageSalary_totMale= sal + cnonc + ss if code==11| code==13| code==15| code==17| code==19| code==21
        gen emp_totFemale = emp if code==12| code==14| code==16| code==18| code==20| code==22
        
        
        .......
        Could you please help how can I write loop for multiple condition for generation multiple variables. Thanks.

        Comment


        • #5
          It's hard to guess what loop you want there, but note for example

          Code:
          if inlist(code, 11, 15, 17, 19, 21) 
          
          
          help inlist()
          More at https://journals.sagepub.com/doi/pdf...867X0600600413

          Comment


          • #6
            Hello!
            I am using Stata 15.1 and i am trying to genetare and label around 15 variables using a loop, however, when i try to run commands Stata returns me too many varibles specified r(103);
            I used the following commands:
            Code:
                local bens BENSX_A BENSX_B BENSX_C BENSX_D BENSX_E BENSX_F BENSX_G BENSX_H BENSX_I ///
                            BENSX_J BENSX_K BENS_L BENS_M
                foreach x of varlist `bens' {
                                        
                                         local name : var label `x'
                                        
                                         gen `name' = (`x'==1)
                                          
                                          }
            I realized that removing the line "local name" puting the command as bellow, the command runs well however i can't understand why it does not run as i wrote it above. I would be too greatful for an explanation about why the code did not work.
            Code:
                local bens BENSX_A BENSX_B BENSX_C BENSX_D BENSX_E BENSX_F BENSX_G BENSX_H BENSX_I ///
                            BENSX_J BENSX_K BENS_L BENS_M
                foreach x of varlist `bens' {
            
                                         gen `x'1= (`x'==1)
                                          
                                          }
            Find bellow an example of the data i am using

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input byte(BENSX_A BENSX_B BENSX_C BENSX_D BENSX_E BENSX_F BENSX_G BENSX_H BENSX_I BENSX_J BENSX_K BENS_L BENS_M)
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            1 2 2 2 2 2 1 2 2 2 1 1 2
            2 2 2 2 2 2 2 2 2 2 2 2 1
            2 2 2 2 2 2 2 2 2 2 2 2 1
            2 2 2 2 2 2 2 2 2 2 2 2 1
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            2 2 2 2 2 2 1 2 2 2 2 2 2
            end
            label values BENSX_A labels18
            label values BENSX_B labels18
            label def labels18 1 "Sim", modify
            label def labels18 2 "Nao", modify
            label values BENSX_C labels19
            label def labels19 2 "Nao", modify
            label values BENSX_D labels20
            label def labels20 2 "Nao", modify
            label values BENSX_E labels21
            label def labels21 2 "Nao", modify
            label values BENSX_F labels22
            label def labels22 2 "Nao", modify
            label values BENSX_G labels23
            label def labels23 1 "Sim", modify
            label def labels23 2 "Nao", modify
            label values BENSX_H labels24
            label def labels24 2 "Nao", modify
            label values BENSX_I labels25
            label def labels25 2 "Nao", modify
            label values BENSX_J labels26
            label def labels26 2 "Nao", modify
            label values BENSX_K labels27
            label def labels27 1 "Sim", modify
            label def labels27 2 "Nao", modify
            label values BENS_L labels28
            label def labels28 1 "Sim", modify
            label def labels28 2 "Nao", modify
            label values BENS_M labels29
            label def labels29 1 "Sim", modify
            label def labels29 2 "Nao", modify
            Best regards

            Ivan Manhique
            Last edited by Ivan Manhique; 22 Apr 2020, 09:46.

            Comment


            • #7
              Code:
              local name : var label `x'
              gen `name' = (`x'==1)
              If your variable label contains spaces, as say "two words", then your command becomes

              Code:
              gen two words = ....
              with the error message you received. This is more likely to work:


              Code:
              local name : var label `x'
              local name - strtoname(`"`name'"')  
              gen `name' = (`x'==1)
              and this is even more likely to work



              Code:
              local lbl : var label `x'
              gen `x'2 = (`x'==1)
              label var `x'2 `"`lbl'"'

              Comment


              • #8
                Dear Mr. Cox,

                Thank you very much for the explanation, both worked perfectly. Just a little note: in the first code maybe it is needed to change the dash (-) for an equal sign (=), like this:
                Code:
                 
                 local name = strtoname(`"`name'"')
                As both commands were useful and necessary, i used both - i am very thankful!

                Best regards

                Ivan Manhique

                Comment


                • #9
                  Dear all contributers,

                  I would like to generate variable from two different list of variables which I define them as locals. What would be the possible code for it ?

                  Here I give an example what I wanted to do:

                  generate distX = m_X - X
                  generate distY = m_Y - Y
                  generate distZ = m_Z - Z
                  generate distT = m_T - T and I have many others..

                  What I tried is that:

                  local list1 X Y Z T
                  local list2 m_X m_Y m_Z m_T

                  foreach i of varlist `List1' {
                  foreach j of varlist `List2' {

                  gen double dista_`i' = `j' - `i'
                  }
                  }

                  However, STATA produces me only distX and it calculates it correctly. For the rest, values are not correct. I think that I missing smth and would be very appreciated if any of you can suggest me a way..

                  Best Regards
                  Kemal
                  Last edited by Kemal Pekcoskun; 03 Aug 2021, 07:20.

                  Comment


                  • #10
                    Code:
                    Here I give an example what I wanted to do:
                    
                    generate distX = m_X - X
                    generate distY = m_Y - Y
                    generate distZ = m_Z - Z
                    generate distT = m_T - T and I have many others..
                    I do not see the need for locals as what differs is the prefix.

                    Code:
                    foreach var in X Y Z T{
                        gen wanted`var'= m_`var'-`var'
                    }

                    Comment

                    Working...
                    X