So, I am trying to consolidate columns of Stata output, but I cannot for the life of me figure out how to do so efficiently.

There are 666 columns of data and 40,000+ rows, but I only need select data from each column based on the group number in Column 1, as seen in the screenshot below.

Additionally, it is not a consistent number of cells from each column.

Is there a function I can use to do this? Or anything to make it simpler? I appreciate any input!