Announcement

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

  • How to writing a code similar as the following code but by region?

    levelsof year, local(years)
    egen flag = tag(id_job year)
    gen stateratio = .
    foreach y of local years {
    summ occ if year == `y' & flag, meanonly
    local odds = `r(mean)'*100
    replace stateratio = `odds' if year == `y'
    }

  • #2
    Or, I need to get the value for the variable stateratio by region by year. What will be the efficient code? Thank you!

    Comment


    • #3
      The original (no data example!) appears equivalent to

      Code:
      egen flag = tag(id_job year)
      rangestat stateratio = occ if flag, int(year 0 0)
      bysort year (stateratio) : replace stateratio = 100 * stateratio[1]
      where rangestat is from SSC. That should make it easier for you.

      Alternatively, this should be quicker than what you have, but slower than rangestat:

      Code:
      egen flag = tag(id_job year)
      egen stateratio = mean(cond(flag, 100 * occ, .)), by(year)
      noting that 100 times the mean of something is the mean of 100 times something).
      Last edited by Nick Cox; 21 Mar 2019, 20:16.

      Comment


      • #4
        Thank you very much! I changed your code to the following:

        egen flag = tag(id_job year)
        rangestat stateratio = occ if flag, int(year 0 0) by(province)
        bysort year (stateratio) : replace stateratio = 100 * (stateratio[1]/100)

        Comment


        • #5
          I don't see much point to multiplying by 100/100.

          Comment


          • #6
            What does [1] in stateratio[1] mean?

            Comment


            • #7
              Without dividing by 100, the number I got equal 4000 if the percentage point Is 40.

              Comment


              • #8
                But from "rangestat stateratio = occ if flag, int(year 0 0) by(province)", the number is 0.4

                Comment


                • #9
                  But 100/100 is 1. Multiplying by 1 is harmless but pointless. As for [1], it is a subscript and identifies the first observation in each group.

                  Note that we can talk about code. We can't talk about your data and results if we can't see them.

                  Comment


                  • #10
                    I change code to:

                    rangestat state = occ if flag, int(year 0 0) by(province)
                    bysort province year (state) : replace state = state*100

                    With state[1] the data change the value of the variable "state" in the first observation (e.g. 0.05) by multiplying 100 (=100*0.05) and the value in the second observation by multiplying 100 and the changed value of the first observation (=100*5).

                    Comment

                    Working...
                    X