Announcement

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

  • assigning values group-wise

    Dear community,

    I have a data set enlisting all previous/current occupations of executives (with start and end dates) which further specifies whether this position was at a quoted company or governmental. If sorted by directorid, you have all his/her previous/current positions grouped together. I now want to check whether his governmental career preceeded his company-career. So far I have generated two variables: gen min_gov = min(start) if companytype=="government" and gen min_quoted = min(start) if companytype=="quoted") and would like to assign these value to the WHOLE group and not just to thie specific row. Is there a way to do this?

    Thanks in advance!

    Marie

  • #2
    Apart from the issue of only getting results in some of the observations, the use of -gen ... = min(...)- will not calculate what you want. In fact, -gen min_gov = min(start)- will just throw a syntax error, becaues the -gen- -min()- function requires multiple arguments. What you want is:

    [code
    by directorid, sort: egen min_gov = min(cond(position== "gov", start, .))
    by directorid, sort: egen min_quoted = min(cond(position == "quoted", start, .))
    [/code]

    As you did not provide example data, I have made this code for imaginary data that includes a string variable called position which contains the value "gov" for observations of government employment and "quoted" for observations of quoted company employment. You will need to modify the code according to how your data is actually structured.

    In the future, when asking for help with code, always show example data, and always use -dataex- to do that. If you are running version 15.1 or a fully updated version 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.



    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      Thank you so much, Clyde! Your Code worked perfectly! The next time I will definitely provide some example data via the dataex-command!

      Comment

      Working...
      X