Announcement

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

  • Multiply entries of a variable.

    I have to multiply different entries of a variable by different number. Please suggest a way for it.
    eg. ID subject Marks
    1 Maths 52
    1 science 45
    1 history 85
    2 Maths 45
    2 science 49
    2 history 85 & so on.
    I have to multiply maths by 2 science by 3 & history by .5 for each entry. Only my data set is about 2000 long.

  • #2
    Code:
    gen multiple = 2*Marks if subject == "Maths"
    replace multiple = 3*Marks if subject == "science"
    replace multiple = 0.5*Marks if subject == "history"
    In the future, please use the -dataex- command to post example data so that if somebody needs to try out code with your data in order to help you they can easily and quickly create a faithful replica of your example. You can get -dataex- by running -ssc install dataex-. Then read the simple instructions at -help dataex-.

    Comment


    • #3
      Thanks, I am new to stata . will adhere to the advice next time. However, since my data set has about 2000 observation it would be highly tiresome to do it. Since, i have data set & multiplier in 2 different .dta files isn't there a way to do it automatically.
      Last edited by nishant kumar; 16 Oct 2016, 00:48.

      Comment


      • #4
        You can create a dataset which contains the variables subject and multiplier, so it will look something like this:
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input str7 subject float multiplier
        "Math"      2
        "Science"   3
        "History"  .5
        "English" .75
        end
        you can then marge many to 1 your original dataset using this dataset (read help merge for more details on merging terminology) and then:
        Code:
        gen new_mark = mark * multiplier
        Alternatively, you can use what Clyde wrote, but in a single command using some logical conditions. though it saves you a few commands, I personally prefer Clyde's method as it's more understandable:
        Code:
        gen new_mark = mark *(2*(subject=="Math")+3*(subject=="Science")+0.5*(subject=="History")+0.75*(subject=="English"))
        Do note that even though you have 2000 observations (or even millions!) - using Clyde's method does not entail 2000 commands - the number of commands is equal to the number of unique conditions, in your case it's the number of unique (or rather, distinct) values for the variable "subject".

        Comment


        • #5
          Thanks Ariel, your answer was very helpful. I understood Clyde's method but i have about 1950 unique observations as it is about food preferences & other daily life items. Your solution helped me for the part where the observations were symmetric. However, i am still struggling for the part where a few observations are missing for certain IDs.
          eg. DATA
          ID Subject Marks
          1 Maths 55
          1 Science 45
          1 History 85
          1 English 54
          2 Maths 78
          2 History 75
          2
          English 74
          3 Maths 98
          3 science 78
          3 english 45

          & so on. Sorry for not using dataex as i am as i am learning to use STATA & dataex command didn't function properly.

          Comment


          • #6
            I don't really understand what's missing or "non-symmetric" in your example

            Comment


            • #7
              There are four entries for ID 1 & 3 (different) for ID 2 & 3. So won't it be a problem to create many to 1 merge as my multiplier would have 4 observations. Thanks for the help.

              Comment


              • #8
                If the multiplier is dependent on subject and not on ID, then there shouldn't be any problem.

                Comment


                • #9
                  Thanks sorted it out after reading about many to one merge. Earlier i was thinking it as a matrix multiplication. Solved my problem.

                  Comment

                  Working...
                  X