Announcement

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

  • making one variable into many

    Dear all,

    I am quite new to stata, and I am stuck at sorting my data.
    I need to change Var4 into many variables, I have attached example to the right side for clear understanding.
    can anyone help please.
    ISIN firm Var4 year ISIN firm Board Functions Product Innovation Product Responsibility Health & Safety Community Vision and Strategy Diversity and Opportunity
    GB00B1YW4409 3I GROUP Board Functions 2002 GB00B1YW4409 3I GROUP xx xx xx xx xx xx xx
    GB00B1YW4409 3I GROUP Board Functions 2003 GB00B1YW4409 3I GROUP xx xx xx xx xx xx xx
    GB00B1YW4409 3I GROUP Product Innovation
    GB00B1YW4409 3I GROUP Product Responsibility
    GB00B1YW4409 3I GROUP Product Responsibility
    GB00B1YW4409 3I GROUP Health & Safety
    GB00B1YW4409 3I GROUP Health & Safety
    GB00B1YW4409 3I GROUP Community
    GB00B1YW4409 3I GROUP Community
    GB00B1YW4409 3I GROUP Vision and Strategy
    GB00B1YW4409 3I GROUP Vision and Strategy
    GB00B1YW4409 3I GROUP Diversity and Opportunity
    GB00B1YW4409 3I GROUP Diversity and Opportunity
    GB00B1YW4409 3I GROUP Board Structure
    GB00B1YW4409 3I GROUP Board of Directors/Board Structure
    GB00B1YW4409 3I GROUP Emission Reduction
    GB00B1YW4409 3I GROUP Emission Reduction
    Regards
    Jas

  • #2
    You can use the tab command with its generate option to do this.
    Code:
    tab(Var4), gen(corporateFunction)

    Comment


    • #3
      Thank you very much for reply Jesse.

      I tried that, but it gives me value of 0 or1. but I need the actual value which my variables has, i.e in year 2002 Board Functions has 40.1 value.

      is there anyway I can do that?

      Comment


      • #4
        I think you are looking to reshape the data then? See help reshape, the syntax is a bit tricky and very case-specific, but it should give you the result you're looking for.

        Comment


        • #5
          I haven't used it yet, but there is the user-written dummieslab, aimed at "converting categorical variable to dummy variables using label names", whose authors are Philippe Van Kerm and Nick Cox.

          According to the information we get when accessing the program:


          dummieslab generates a set of dummy variables from a categorical
          variable. One dummy variable is created for each level of the
          original variable. Names for the dummy variables are derived from
          the value labels of the categorical variable.
          Definitely, it seems to dovetail with your needs.
          Best regards,

          Marcos

          Comment


          • #6
            As Jesse pointed out, jaskaran's original example looks like it must have a reshape (which is normally from long to wide data representations], but on second look it appears to be taking a long data set that has multiple observations for each firm-year and making it into a long data set with one observation per firm year.

            By the way, jaskaran, you don't have year in your original data - you must have it somewhere to get it in the new data.

            You may be able to do with with reshape. Alternatively, if your data has exactly the same structure for each firm, you can do it with a loop and then drop excess observations.

            With

            +----------------------------------------------------------------------------+
            | year ISIN firm Var4 var5 |
            |----------------------------------------------------------------------------|
            1. | 2002 GB00B1YW4409 3I GROUP Board Functions 1 |
            2. | 2003 GB00B1YW4409 3I GROUP Board Functions 2 |
            3. | 2002 GB00B1YW4409 3I GROUP Product Innovation 3 |
            4. | 2002 GB00B1YW4409 3I GROUP Product Responsibility 4 |
            5. | 2003 GB00B1YW4409 3I GROUP Product Responsibility 5 |
            |----------------------------------------------------------------------------|
            6. | 2002 GB00B1YW4409 3I GROUP Health & Safety 6 |
            7. | 2003 GB00B1YW4409 3I GROUP Health & Safety 7 |
            8. | 2002 GB00B1YW4409 3I GROUP Community 8 |
            9. | 2003 GB00B1YW4409 3I GROUP Community 9 |
            10. | 2002 GB00B1YW4409 3I GROUP Vision and Strategy 10 |
            |----------------------------------------------------------------------------|
            11. | 2003 GB00B1YW4409 3I GROUP Vision and Strategy 11 |
            12. | 2002 GB00B1YW4409 3I GROUP Diversity and Opportunity 12 |
            13. | 2003 GB00B1YW4409 3I GROUP Diversity and Opportunity 13 |
            14. | 2002 GB00B1YW4409 3I GROUP Board Structure 14 |
            15. | 2003 GB00B1YW4409 3I GROUP Board of Directors/Board Structure 15 |
            |----------------------------------------------------------------------------|
            16. | 2002 GB00B1YW4409 3I GROUP Emission Reduction 16 |
            17. | 2003 GB00B1YW4409 3I GROUP Emission Reduction 17 |
            +----------------------------------------------------------------------------+


            You might do something like:

            sort year Var4
            *is Board Structure the same as Board of Directors/Board Structure?*

            foreach var in bf bs c {
            g `var'=.
            }

            replace bf=var5 if Var4=="Board Functions"

            forvalues i=1/9 {
            replace bs=var5[_n+`i'] if firm==firm[_n+`i'] & ///
            (Var4=="Board Structure" | Var4=="Board of Directors/Board Structure")
            replace c= var5[_n+`i'] if firm==firm[_n+`i'] & Var4=="Community"
            }
            collapse bf bs c , by(year firm)

            Comment


            • #7
              Thank you very much. I have solved this problem. Thank you once again.

              Comment

              Working...
              X