I have a dataset with three variables: id (the id of a company), parent_id (the id of that company's parent company) and year. I have another dataset with just the parent companies (which are the companies that have id = parent_id). I need to add to this second dataset the companies from the first dataset by order. By order I mean they have different levels (level 1 have parent_id = id from the parent company; level 2 companies have parent_id = id from companies in level 1, and so on). So, I need to add all these companies by levels and by year.
I have no idea of how to do it.