Announcement

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

  • Computations between variables of the same subscript?

    Hello Stata experts,

    I have multiple variable sets. Each set consists of two variables, e.g., var_a_1, var_a_2, var_b_1, var_b_2, ... var_z_1, var_z_2. I want to compute the difference within the same set (e.g., var_a_1-var_a_2) for all variable sets. There must be a way to do all the computation at once. Could you please help me? Thank you!

    Best,
    Ji

  • #2
    Try this:
    Code:
    foreach x in `c(alpha)' {
        gen delta_`x' = var_`x'_1 - var_`x'_2
    }

    Comment


    • #3
      Hi Clyde, thanks for this. Just to understand the code, will it work if the sequence of variables are different i.e. var_a_1 - var_a_2 and var_z_1 - var_z_2 ?
      Roman

      Comment


      • #4
        Roman, how is it different from your original question?

        If x is causing confusion then note that Clyde's code is equivalent to:
        Code:
        foreach subscript in a b c d e f g h i j k l m n o p q r s t u v w x y z {
          gen delta_`subscript' = var_`subscript'_1 - var_`subscript'_2
        }
        Which makes it more explicit how the loop works: the universe is given by the system enumeration of all letters and the macro "subscript" is used to stand out from one lettered indices.
        If your subscripts are different, e.g. "north, south, east, west" - just enumerate them in the foreach statement.

        Best regards, Sergiy Radyakin.

        Comment


        • #5
          Hi Sergiy, Many thanks. My apology for being an intruder in the thread. I just wanted to understand the hieroglyphic codes from Clyde which are now clear from your explanation. Hope the original poster got the answer he/she is looking for. Thanks again.
          Roman

          Comment


          • #6
            My apologies to all that I didn't frame my question clearly. The variables are not named with letter subscripts to differentiate them (But thanks Clyde and Sergiy. You guys helped me learn c(alpha) and how to deal with variables with letter subscripts. Amazing!). Here are the variables I am working with: var1_1 var1_2, var2_1 var2_2,....var10_1 var10_2. I still can't get a correct foreach code to run. Thank you for helping out!


            Comment


            • #7
              I came up with a cumbersome way to do this. I reshaped the data to long, and then used

              foreach var in var1......var10 {
              by itemid: gen dif_`var'=`var'[1]-`var'[2]
              }

              itemid is the id for the each survey question. It worked. But I really want to learn the foreach code without shaping the data. Thanks in advance for the help.

              Perhaps a little more detail would help clarify the task I am doing. I am working with a faculty survey. The data are summary results of each survey question by mean, sd, %strongly agree, % agree, etc. The summary stats are provided for each faculty group (education, psychology, engineering...tenured, assistant professors...male, female, etc), because no raw data are provided to ensure anonymity. So I am treating the summary stats for each faculty group as an observation. One of the reports I need to produce is the summary table for each group, plus showing the difference between the faculty group and the university average.




              Comment


              • #8
                I understand you want:
                Code:
                forvalues i = 1/10 {
                    gen delta_`i' = var_`i'_1 - var_`i'_2
                }
                You should:

                1. Read the FAQ carefully.

                2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

                3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

                4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

                Comment


                • #9
                  Thanks Roberto! Will follow when posting future questions!

                  Comment

                  Working...
                  X