2
votes

I'm a new user of Stata and I'm trying to understand how it executes commands. I'm facing trouble in restructuring data from its present format to a panel data format.

I'm using firm level micro-data which, for example, contain firm id, last avail year (latest year for which data was collected from that firm) and turnover (REV_LAY-0 = turnover from last avail year - 0, REV_LAY-1 = turnover from last avail year - 1 and so on).

The present data format is the following:

Present data format

The required panel format looks like this:

Required panel data format

In SAS, I would do the following in a loop:

if last_avail_yr=2016 then do;
rev_2016=rev_lay-0;
rev_2015=rev_lay-1;
rev_2014=rev_lay-2;
rev_2013=rev_lay-3;
end;

But I'm not quite sure how to do it Stata. I tried using an if statement with a forvalues loop to achieve a similar result, but it didn't work out well.

Example data can be found below:

MARK BvD_ID       LAST_AVAIL_YR REV_LAY0 REV_LAY1 REV_LAY2 REV_LAY3 REV_LAY4
437  ESA22001721  2016          27689    32097    28992    35868    36493
438  ESF23212103  2015          26786    52095    33023    29493    40368
439  ESB45426806  2012          22072    14864    12877    15330    6403
440  ESA45039294  2015          26700    23387    21104    21272    20002
441  ESB76638790  2016          27480    24303    10699    .        .

Can anyone help me with the Stata code for this problem?

2
Please post example data as you did SAS code, as text that others can copy and paste. The guidelines at statalist.org/forums/help#stata generally apply here too. - Nick Cox
I posted the example data in my post. As Mauricio pointed, the variable names described in the question are not valid names in stata. I put the names in that way to make the question more clearer. - Avinash Kumar
Still pretty unclear to me. For example, I see no firm_id above but it's in your images and @Mauricio is referring to it. - Nick Cox
firm_id is BvD_ID, apologies for changing the name of variables and not making it clear in the post - Avinash Kumar

2 Answers

1
votes

rev_lay-0 and so on are not valid names in Stata, so I assume they would be named rev_lay_0 and so on. Given that, the following should do the trick:a

reshape long rev_lay_, i(firm_id last_avail_yr) j(id)
by firm_id last_avail_yr: gen yr = last_avail_yr - _n + 1
keep firm_id last_avail_yr rev_lay_ yr
reshape wide rev_lay_, i(firm_id last_avail_yr) j(yr)
1
votes

Although the accepted answer gives the OP what was asked for, the desired data layout is not very useful in Stata. A reshape long alone would produce a simple layout which is much, much better for most data management, all graphics and all statistical modelling undertaken with panel data in Stata:

clear 
input MARK str11 BvD_ID       LAST_AVAIL_YR REV_LAY0 REV_LAY1 REV_LAY2 REV_LAY3 REV_LAY4
437  ESA22001721  2016          27689    32097    28992    35868    36493
438  ESF23212103  2015          26786    52095    33023    29493    40368
439  ESB45426806  2012          22072    14864    12877    15330    6403
440  ESA45039294  2015          26700    23387    21104    21272    20002
441  ESB76638790  2016          27480    24303    10699    .        .
end 

reshape long REV_LAY , i(BvD_ID) 

gen YEAR = LAST_AVAIL_YR - _j
drop if missing(REV_LAY)
drop _j LAST

list, sepby(BvD_ID)

      +-------------------------------------+
     |      BvD_ID   MARK   REV_LAY   YEAR |
     |-------------------------------------|
  1. | ESA22001721    437     27689   2016 |
  2. | ESA22001721    437     32097   2015 |
  3. | ESA22001721    437     28992   2014 |
  4. | ESA22001721    437     35868   2013 |
  5. | ESA22001721    437     36493   2012 |
     |-------------------------------------|
  6. | ESA45039294    440     26700   2015 |
  7. | ESA45039294    440     23387   2014 |
  8. | ESA45039294    440     21104   2013 |
  9. | ESA45039294    440     21272   2012 |
 10. | ESA45039294    440     20002   2011 |
     |-------------------------------------|
 11. | ESB45426806    439     22072   2012 |
 12. | ESB45426806    439     14864   2011 |
 13. | ESB45426806    439     12877   2010 |
 14. | ESB45426806    439     15330   2009 |
 15. | ESB45426806    439      6403   2008 |
     |-------------------------------------|
 16. | ESB76638790    441     27480   2016 |
 17. | ESB76638790    441     24303   2015 |
 18. | ESB76638790    441     10699   2014 |
     |-------------------------------------|
 19. | ESF23212103    438     26786   2015 |
 20. | ESF23212103    438     52095   2014 |
 21. | ESF23212103    438     33023   2013 |
 22. | ESF23212103    438     29493   2012 |
 23. | ESF23212103    438     40368   2011 |
     +-------------------------------------+