Hi all,
I have a question about my data. I would like to generate a new variable which I call here MG_new. There have been changes in the sec of the ID variable. So I need the HG also to be changed.
I used this code and it gave me something not what I wanted (I am aware that it is not the right code but was trying to figure out a way to do this in a quick way!)
The optimal result I could get is something like this:
I have a very large dataset, and it is kinda cumbersome to use "replace .. if" for every single observation I want to change.
Thanks for your answers in advance!
Best wishes
JLi
I have a question about my data. I would like to generate a new variable which I call here MG_new. There have been changes in the sec of the ID variable. So I need the HG also to be changed.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str1 id_comp byte(sec1 mg sec_new) "A" 50 4 60 "B" 61 1 17 "C" 10 2 20 "D" 17 3 17 "E" 20 5 50 "F" 60 1 61 "G" 12 5 10 end
Code:
gen MG_new=. replace MG_new=mg if sec1==sec_new
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str1 id_comp byte(sec1 mg sec_new) float MG_new "A" 50 4 60 . "B" 61 1 17 . "C" 10 2 20 . "D" 17 3 17 3 "E" 20 5 50 . "F" 60 1 61 . "G" 12 5 10 . end
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str1 id_comp byte(sec1 mg sec_new) float MG_new "A" 50 4 60 1 "B" 61 1 17 3 "C" 10 2 20 5 "D" 17 3 17 3 "E" 20 5 50 4 "F" 60 1 61 1 "G" 12 5 10 2 end
Thanks for your answers in advance!
Best wishes
JLi
Comment