Announcement

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

  • Generate a count Variable from three variables

    Hello

    I have the variables "year" (2009-2019), "company key" and "investment type" (e.g. green, biotechnology, ...) and want to create a count variable that shows me how many green investments have been made per company per year. How can I do that?

    E.g. Company X had 25 green investments in the year 2009.

    Thank you for your help!

    Best,
    Jana

  • #2
    Welcome to Statalist. The code depends on whether "investment type" is a string variable or a numeric variable with value labels. If the former

    Code:
    bys company_key year: egen wanted= total(investmet_type=="green")
    If numeric

    Code:
    lab list
    to see what value is labeled "green". Assuming green=2

    Code:
    bys company_key year: egen wanted= total(investmet_type==2)
    Last edited by Andrew Musau; 18 Oct 2021, 11:24.

    Comment


    • #3
      Jana,
      Just an addition to Andrew's code. If investment_type has many values, you may also consider the following:
      clear
      input comp_key str20 inv_type year
      3 "green" 2009
      2 "biotechnology" 2010
      end

      encode inv_type, gen( inv_type_)
      levelsof inv_type_, local(levels)
      foreach x in `levels' {
      bys comp_key year: egen wanted_`x'= total(inv_type_==`x')
      }

      Comment


      • #4
        Thank you so much

        Comment

        Working...
        X