Hello everyone,

I want to reshape my dataset from wide to long format and keep the label information as a new variable.

This is what my data looks like in long format:
Code:
CompanyName                                      _v1                    _v2                   _v3                  _v4
"GSW Immobilien AG"                                          0       5894403840 -1.88679245283019 5910893287.23354
"Deutsche Rohstoff AG"                        4.28571428571428       74193506.2  9.80392156862745         71144458
"Draegerwerk AG & Co KGaA"                   -.134408602150549 1392782583.91451 -.626666666666659 1407410211.36748
"Tonkens Agrar AG"                            .442477876106205 7521354.29451648                 0 7519657.36964375
"BHB Brauholding Bayern Mitte AG"            -13.9240506329114          8432000  16.1764705882353          9796000
"Deutsche Konsum REIT AG"                    -.324675324675329 538890934.198159 -.323624595469249 542916002.194247
"BRAIN Biotech AG"                            2.40700218818379      185902329.6 -1.72043010752688      181532830.4
"Senvion SA"                                  10.4972375690608       14623376.6  .555555555555556     13234155.823
"Lion E Mobility AG"                         -6.63265306122449      36719436.78 -3.20987654320987      39327921.36
"va Q tec AG"                                -6.35179153094462      376323182.5 -.486223662884934      401847711.4
"Bitcoin Group SE"                           -8.73180873180874        2.195e+08 -1.83673469387755        2.405e+08
"Shop Apotheke Europe NV"                    -.846354166666656     2731518928.3 -4.77371357718538     2754834585.6
"Medios AG"                                  -2.77777777777778        709274685                 0        729539676
end
_v1 is return of day one (the label of the variable is the date, here it is 5/11/2021), _v2 is the market cap of the same day (again, the label is 5/11/2021).

I reshaped the data using the following command:

Code:
gen long obs_no = _n
reshape long _v, i(obs_no) j(_j)
gen which_var = "return" if mod(_j, 2) == 1, before(_v)
replace which_var = "market_cap" if missing(which_var)
gen firm_num = ceil(_j/2)
drop  _j
reshape wide _v, i(firm_num obs_no) j(which_var) string
rename _v* *
drop obs_no
unfortunately, I was not able yet to create a new variable with the date.
This is what my data looks like after reshaping it:

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
firm_num  market_cap      return        CompanyName
1       5894403840                 0 "GSW Immobilien AG"                         
1       74193506.2  4.28571428571428 "Deutsche Rohstoff AG"                      
1 1392782583.91451 -.134408602150549 "Draegerwerk AG & Co KGaA"                  
1 7521354.29451648  .442477876106205 "Tonkens Agrar AG"                          
1          8432000 -13.9240506329114 "BHB Brauholding Bayern Mitte AG"           
1 3931445305.56908 -2.66106442577032 "Stroeer SE & Co KGaA"                                                        
1 11003685830.4026 -1.27868852459017 "Uniper SE"                                 
1      36719436.78 -6.63265306122449 "Lion E Mobility AG"                        
1      376323182.5 -6.35179153094462 "va Q tec AG"                               
1        2.195e+08 -8.73180873180874 "Bitcoin Group SE"                          
1     2731518928.3 -.846354166666656 "Shop Apotheke Europe NV"                   
1        709274685 -2.77777777777778 "Medios AG"                                 
end
What I would like is a new variable with the label information. Hence, for firm_num=1 it would be 5/11/2021, so the same date as for variable _v1 and _v2.