I'm fairly new to Stata 17, and Stata in general and I had a question about replacing all values of certain variables. I have a panel data set (time-series cross-sectional data) with country-years as the unit of observation. My data set spans 26 countries from 2019-2021. However, for most of the variables in 2021, there are no observations (I guess you could call that missing data). However, I consider most of the variables to be constant, and thus I want to replace all the missing values in 2021 with values of all the variables from 2020 accordingly. Here is a snapshot of my data:
Code:
* Example generated by -dataex-. For more info, type help dataex clear input str32 country_name double(year v2x_cspart v2x_egaldem) "Sweden" 2020 .943 .815 "Sweden" 2021 . . "Switzerland" 2020 .969 .825 "Switzerland" 2021 . . "Japan" 2020 .724 .741 "Japan" 2021 . . "United States of America" 2020 .979 .63 "United States of America" 2021 . . "Portugal" 2020 .785 .764 "Portugal" 2021 . . "Canada" 2020 .956 .764 "Canada" 2021 . . "Australia" 2020 .864 .716 "Australia" 2021 . . "France" 2020 .884 .766 "France" 2021 . . "Germany" 2020 .982 .809 "Germany" 2021 . . "Ireland" 2020 .97 .788 "Ireland" 2021 . . "Italy" 2020 .921 .785 "Italy" 2021 . . "Netherlands" 2020 .904 .781 "Netherlands" 2021 . . "Spain" 2020 .907 .811 "Spain" 2021 . . "United Kingdom" 2020 .957 .746 "United Kingdom" 2021 . . "Austria" 2020 .937 .764 "Austria" 2021 . . "Belgium" 2020 .951 .824 "Belgium" 2021 . . "Denmark" 2020 .987 .87 "Denmark" 2021 . . "Finland" 2020 .972 .802 "Finland" 2021 . . "Greece" 2020 .891 .715 "Greece" 2021 . .
I have tried the following code successfully to duplicate one variable's values into another year:
Code:
by country_id (year), sort: replace v2x_egaldem = v2x_egaldem[1]
Best,
Nathan
0 Response to Panel data replace all values
Post a Comment