Announcement

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

  • General looping and storing a value.

    Hello Everyone
    I have following data with three variables
    1. State name
    2. months data
    3. Median for each month
    Now I would like divide Median of 0 month for every other month
    for example I have median of state A1 in month 0 is 3.7 that I need to divide for state A1 in month 1, 2, and so on., (3.7 is fixed for rest of the months for that state).
    Likewise I have 25 states with 36 months data. I would like to go through a loop, Please advice me.
    Need to create a new variable new_var that can take values as from 11 to 20 it has to be 4.2/3.7 and from 21-30 it has to be 5.1/3.7 like wise.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str2 state byte months float median
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 0  3.7
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 1  4.2
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 2  5.1
    "A1" 3  5.6
    "A1" 3  5.6
    "A1" 3  5.6
    "A1" 3  5.6
    "A1" 3  5.6
    "A1" 3  5.6
    "A1" 3  5.6
    "A1" 3  5.6
    "A1" 3  5.6
    "A1" 3  5.6
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 0  3.3
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 1 4.35
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 2  4.8
    "A2" 3  5.6
    "A2" 3  5.6
    "A2" 3  5.6
    "A2" 3  5.6
    "A2" 3  5.6
    "A2" 3  5.6
    "A2" 3  5.6
    "A2" 3  5.6
    "A2" 3  5.6
    "A2" 3  5.6
    end
    Thanks for you help.

  • #2
    The median for month 0 is in

    Code:
    bysort state (months) : gen reference = median[1]
    and so you can use that in any later division. I suspect that you want to divide by it. In fact you don't even need that new variable.

    Code:
    bysort state (months) : gen wanted = median/median[1]
    or its reciprocal appears to be what you are asking for. No loops needed,

    Comment


    • #3
      Your data is strange: there are numerous observations that are exact duplicates of each other. Perhaps the real data set has other variables which differ among these observations and you have chosen not to show those. If the real data set really contains numerous observations that are exact duplicate, that is often indicative of an error in the data management that created the data set, and you should look into that and fix any problems you find.

      Assuming your data set is correct, for your question to make sense, all observations having the same value of A1 and months == 0 must have the same value for median. (Otherwise, it would be impossible to know which value of median to use for the division.) The following code verifies this assumption, and if it is correct, does what you ask for.

      Code:
      by state months, sort: assert median[1] == median[_N] if months == 0
      by state (months): gen wanted = median/median[1]
      Added: Crossed with #2.

      Comment


      • #4
        Try this
        Code:
        bysort state (months): gen new_var = median/median[1] if months!=0
        Added: crossed with #2 and #3. The solutions are pretty much the same. The if condition in my solution exists because you didn't specify what value you want the new variable to take when months is 0.
        Last edited by Hemanshu Kumar; 18 Jan 2023, 11:53.

        Comment


        • #5
          What we tell you three times is true. Compare the principle of. the Bellman in Lewis Carroll, The Hunting of the Snark. Unsurprisingly, the late American mathematician Richard Ernest Bellman used the original quotation somewhere.

          Comment


          • #6
            Nick Cox Clyde Schechter Hemanshu Kumar Thank you alll for such qucik response. All the code worked well. Thanks once again,

            Comment

            Working...
            X