Announcement

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

  • generating a varibale

    Dear Stata Users,

    I have a data example as shown below and I have a question about generating one of my main variables.

    There are two IDs: 10 and 20, unit_a2 is just the first two numbers of unit_a4. unit_b comes from external data that I merged with my initial dataset.
    • I need to generate the variable "department_prod". This is what the department in a company produces.
    • The variable unit_a2 for ID=10 has values 36 and 40.
    • So the "department_prod" must be equal to 1, otherwise, it must be equal to zero.
    • If unit_a4=unita2, then the dummy is equal to 2 only if the ID (in this case 10) has more than one unit (3635 and 3640) that is within the same 4-digit unit number which starts by 36.
    • In the case of ID=20, the "department_prod"=0 when unit_a4=unita2 because unit_4a =3620 and 2811 so they do not have the same 2-digit 36!= 28.
    I cannot give an example of my data due to technical/legal reasons. I would like to know how to generate "department_prod".

    Thank you very much,
    JadLi



    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id int(year unit_a4) byte(unit_a2 unit_b department_prod)
    10 2004 3635 36 36 2
    10 2004 3635 36 37 0
    10 2004 3635 36 38 0
    10 2004 3635 36 39 0
    10 2004 3635 36 40 1
    10 2004 3640 36 36 2
    10 2004 3640 36 37 0
    10 2004 3640 36 38 0
    10 2004 3640 36 39 0
    10 2004 3640 36 40 1
    10 2004 3640 36 41 0
    10 2004 4019 40 36 1
    10 2004 4019 40 37 0
    10 2004 4019 40 38 0
    10 2004 4019 40 39 0
    10 2004 4019 40 40 2
    10 2004 4019 40 41 0
    20 2004 3620 36 36 0
    20 2004 3620 36 37 0
    20 2004 3620 36 38 0
    20 2004 3620 36 39 0
    20 2004 3620 36 40 1
    20 2004 3620 36 41 0
    20 2004 3620 36 42 0
    20 2004 2811 28 36 1
    20 2004 2811 28 37 0
    20 2004 2811 28 38 0
    20 2004 2811 28 39 0
    20 2004 2811 28 40 1
    20 2004 2811 28 41 0
    20 2004 2811 28 42 0
    end
    ------------------ copy up to and including the previous line ------------------


  • #2
    Your explanation is full of mistakes (esp. in the names of variables) which are making it hard to understand what you want to do. Could you please edit your post?

    Also, does your current department_prod variable show values that you would like it to take, i.e. are we trying to generate a variable that looks exactly like this one?

    Comment


    • #3
      Thank you for your reply: I could not edit the post, so I am rewriting it below.

      There are two IDs: id = 10 and id = 20, unit_a2 is just the first two numbers of unit_a4. The variable unit_b comes from external data that I merged with my initial dataset.
      • I need to generate the variable "department_prod". In the example below, I created the variable "department_prod" just an example of how I want it to be, but I need to generate it myself in Stata.
      • The variable "unit_a2" for id=10 has values 36 and 40.
      • For id = 10, unit_b then has two values, 36 and 40 that are in variable "unit_a2". Thus, for these observations, the "department_prod" variable must be equal to 1, otherwise equal to zero.
      • When unit_b = unit_a2 the value of "department_prod" must be equal to 2 or 0.
      • When the id has more than one unit that starts with the same 2-digit, then "department_prod" is equal to 2. In this case, id = 10 has unit_a4 =3635 and 3640, both correspond to 36 (first two digits) in unit_a2.
      • In the case of id = 20, "department_prod" = 0 because it has units that have different first-2digit such as 3620 and 2811 in uit_a4 (this corresponds to 36 and 28 in unit_a2).
      I hope this explanation helps. Let me know please if I still need to edit or clarify anything.

      Regards,
      JLi

      Comment

      Working...
      X