Announcement

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

  • Why the program does not recognize my variables in egen syntax?

    Dear all experts,

    I try to calculate the average of some variables for each observation.
    My code is:

    Code:
    egen laporta_ave = mean(con_of_corr rul_of_law reg_qua gov_eff poli_stab voice_acc), by(obsnum) /*The "by(obsnum)" option tells Stata to calculate the average for each observation.*/
    I am sure the variables in the mean() exist in my daatset. I am wondering why they turn out :


    Code:
    .                  egen laporta_ave = mean(con_of_corr rul_of_law reg_qua gov_eff poli_stab voice_acc), by(obsnum) /*The "by(obsnum)" option tells Stata to calculate the average for each observation.*/
    con_of_corrrul_of_lawreg_quagov_effpoli_stabvoice_acc invalid name
    r(198);
    Thank you so much and best regards


  • #2
    It is not clear what you want to do here. -egen, mean()- expects a single expression as an argument, you are supplying 3 variables. You might be thinking of -egen, rowmean()-.

    Comment


    • #3
      Phuc:
      if you have a cross-sectional dataset, the mean of a given variable for a given observation is the value of that variable (if observed).
      Are you dealing with a cross-sectional or a panel dataset?
      Do you need the mean for different variables for the same observation?
      Do you need the mean of a given variables for a given panel?
      Please provide interested listers with more details. Thanks.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        Originally posted by Joro Kolev View Post
        It is not clear what you want to do here. -egen, mean()- expects a single expression as an argument, you are supplying 3 variables. You might be thinking of -egen, rowmean()-.
        Thank you Joro Kolev , I have tried many ways around but not yet fixed it, so I used the conventional approach that I divide sum of 6 variable by 6. Quite less aesthetic but direct, I deem.

        Thank you Carlo Lazzaro, regarding my data, it is a panel dataset, I need the mean for various variable of each observation. In general, it similar to getting sum of 6 variables and devide by 6. And I need the mean of each observation in the panel

        Thank you so much for taking consideration my question.

        Comment


        • #5
          You are not providing a data sample using -dataex- and you are not explaining what you want with a reference to this dataset...

          But the way how you explain it, it seems to me that you want

          Code:
          egen mymean = rowmean(v1-v6)
          and yes, of course you can also do it manually, as long as you deal with less than 30 variables say.

          -egen, rowmean()- is powerful because it can take varlists.

          Comment


          • #6
            Originally posted by Joro Kolev View Post
            You are not providing a data sample using -dataex- and you are not explaining what you want with a reference to this dataset...

            But the way how you explain it, it seems to me that you want

            Code:
            egen mymean = rowmean(v1-v6)
            and yes, of course you can also do it manually, as long as you deal with less than 30 variables say.

            -egen, rowmean()- is powerful because it can take varlists.
            Thank you so much, it is exactly what I am looking for

            Comment

            Working...
            X