I am working with a dataset about economic activities in Brazil such as the one below (going through A to U in variable 'secao')
id | secao | descricao_secao |
1 | A | Agricultura e Pecuaria |
2 | ||
3 | ||
4 | ||
5 | B | Industria Extrativa |
6 | ||
7 | ||
8 | C | Industria de Transformacao |
9 | ||
10 | ||
11 | ||
12 |
I would like to replace the variable 'descricao_secao' to be like
id | secao | descricao_secao |
1 | A | Agricultura e Pecuaria |
2 | Agricultura e Pecuaria | |
3 | Agricultura e Pecuaria | |
4 | Agricultura e Pecuaria | |
5 | B | Industria Extrativa |
6 | Industria Extrativa | |
7 | Industria Extrativa | |
8 | C | Industria de Transformacao |
9 | Industria de Transformacao | |
10 | Industria de Transformacao | |
11 | Industria de Transformacao | |
12 | Industria de Transformacao |
The interest here is to find a way of replacing the value of the current observation for variable 'descricao_secao' with the last observation to fill the gaps until the next different value of the variable 'secao'.
I ran the following command but I couldn't manage to solve the issue:
bys id: replace descricao_secao=descricao_secao[_n-1] if secao[_n-1]!="" & _n!=1
//if it worked, then I would just replace the first obs after that
Stata throws no error but it does not replace any value.
Could you help me to find a solution for that?
Thank you very much!
To import the original dataset:
clear
input id str10 secao str30 descricao_secao
1 "A" "Agricultura e Pecuaria"
2 "" ""
3 "" ""
4 "" ""
5 "B" "Industria Extrativa"
6 "" ""
7 "" ""
8 "C" "Industria de Transformacao"
9 "" ""
10 "" ""
11 "" ""
12 "" ""
end
0 Response to How to create a variable that replaces the current observation's value with the last value based on variying criteria
Post a Comment