Announcement

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

  • About Foreach or Forvalue

    Hi! I am trying to create seven summary variables named den_1 to den_7 to simplify the results I have from 34 variables named total1-total34. Although there are 34 different groups, each observation actually only belongs to 7 groups at most. The number of groups that each belongs is shown by the variable group_n.

    Enquiry:
    Is possible that the -foreach- command or -forvalue- command can search over the 34 variables, take out the values, and put it in a new sets of variables (den_1- den_7)?


    Data example skipping variables for simplification:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(total1 total2 total3 total32 total33 total34 group_n)
    208 207  . . . . 4
    208 207  . . . . 4
      .   .  . . . . 0
      .   .  . . . . 1
    208 207  . . . . 4
      .   .  . . . . 1
      .   .  . . . . 4
      .   .  . . . . 0
      .   .  . . . . 2
      .   .  . . . . 1
      .   .  . . . . 0
      .   .  . . . . 2
      .   .  . . . . 0
      .   .  . . . . 1
      .   .  . . . . 0
    208 207  . . . . 4
      .   .  . . . . 2
      .   .  . . . . 3
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 2
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . 5 . 2
    208 207  . . . . 5
      .   .  . . . . 0
      .   .  . . . . 3
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 2
      .   .  . . . . 2
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 2
      .   .  . . . . 0
    208 207 50 . . . 5
      .   .  . . . . 1
      .   .  . . . . 3
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 0
    208 207  . . . . 5
      .   .  . . . . 2
      .   .  . . . . 2
      .   .  . . . . 1
      .   .  . . . . 1
      .   .  . . . . 2
    208 207  . . . . 5
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 1
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 3
      .   .  . . . . 2
      .   .  . . . . 0
      .   .  . . . . 2
      .   .  . . . . 0
    208 207  . . . . 5
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 1
      .   .  . . . . 2
    208 207  . . . . 6
      .   .  . . . . 4
      .   .  . . . . 2
    208 207  . . . . 6
      .   .  . . . . 2
      .   .  . . . . 2
      .   .  . . . . 3
    208 207  . . . . 5
      .   .  . . . . 3
      .   . 50 . . . 2
      .   .  . . . . 0
      .   .  . . . . 2
      .   .  . . . . 0
      .   .  . . . . 3
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 0
      .   . 50 . . . 2
      .   . 50 . . . 3
      .   .  . . . . 0
      .   .  . . . . 3
      .   .  . . . . 0
      .   .  . . . . 0
    208 207  . . . . 5
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 2
    208 207  . . . . 4
      .   .  . 5 . . 3
      .   .  . . . . 2
      .   .  . . . . 3
      .   .  . . . . 2
      .   .  . . . . 5
      .   .  . . . . 0
      .   .  . . . . 0
      .   .  . . . . 0
    end
    Thank you all very much!!

  • #2
    Esther:
    assuming that you're interested in the mean of those variables, you may want to consider something along the following lines (groups from 0 to 7, bounds included):
    Code:
    . forvalues i = 0(1)7 {
      2. egen wanted`i'=rowmean(total*)
      3.  }
    Obvuously, what above can be tweaked according to your reseach goals, if different.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Thank you for your reply, Carlo! Is there a way to just copy the numbers to one of the new variables?
      As in, if searching isn't a problem, I could put
      Code:
       replace wanted1 = total1
      replace wanted2 = total4

      Comment


      • #4
        Esther:
        assuming that you're interested in the mean of those variables, you may want to consider something along the following lines (groups from 1 to 7, bounds included):
        Code:
        . forvalues i = 1(1)7 {
          2. egen wanted`i'=rowmean(total*) if group==`i'
          3. }
        What above amends my previous code and, hoperfully, could save your time.
        Last edited by Carlo Lazzaro; 26 Nov 2022, 08:41.
        Kind regards,
        Carlo
        (Stata 19.0)

        Comment


        • #5
          I don't see any immediate point in creating 7 new variables.

          The results for different groups remain separate with

          Code:
          egen rowmean = rowmean(total*) 
          and if later you really need distinct new variables you can fire up separate.

          Comment


          • #6
            Thank you very much indeed for your help, Nick Cox and Carlo Lazzaro!!

            Comment

            Working...
            X