Hello!
I'm not the best at stata and need help how to code a new variable in my panel dataset (2007-2023). The purpose of the variable is to tell me how depressed/gloomy someone was feeling during a the latest election year.
I have a variable for "ID" (each person), "year" (some people are not participating every year) and "gloomy" (scale from 1-7). The election years were 2010, 2012, 2017, 2021.
I want the new variable "DepressedElection" to tell me the value of "gloomy" for the latest election year, and if the person was not participating in the survey in the lastest election year I want the "DepressedElection" to be missing.
So to be concrete: If the person with ID "..." was participating in the survey year 2010, the "DepressedElection" should tell the value of the variable "gloomy" for 2010. If that same person, with the same "ID" was participating 2011, the "DepressedElection" should tell the value of the variable "gloomy" for 2010 for that specific person/"ID". If that same person, with the same "ID" was participating 2012, the "DepressedElection" should tell the value of the variable "gloomy" for 2012, since there was a new election in 2012, for that specific person/"ID". If that same person, with the same "ID" was participating 2013, the "DepressedElection" should tell the value of the variable "gloomy" for 2012 for that specific person/"ID". If that same person, with the same "ID" was participating 2014, the "DepressedElection" should tell the value of the variable "gloomy" for 2012 for that specific person/"ID". If that same person, with the same "ID" was participating 2015, the "DepressedElection" should tell the value of the variable "gloomy" for 2012 for that specific person/"ID". If that same person, with the same "ID" was participating 2016, the "DepressedElection" should tell the value of the variable "gloomy" for 2012 for that specific person/"ID". If that same person, with the same "ID" was participating 2017, the "DepressedElection" should tell the value of the variable "gloomy" for 2017 for that specific person/"ID", since there was a new election. If that same person, with the same "ID" was participating 2018, the "DepressedElection" should tell the value of the variable "gloomy" for 2017 for that specific person/"ID". If that same person, with the same "ID" was participating 2019, the "DepressedElection" should tell the value of the variable "gloomy" for 2017 for that specific person/"ID". If that same person, with the same "ID" was participating 2020, the "DepressedElection" should tell the value of the variable "gloomy" for 2017 for that specific person/"ID". If that same person, with the same "ID" was participating 2021, the "DepressedElection" should tell the value of the variable "gloomy" for 2021 for that specific person/"ID", since it was a year with an election. If that same person, with the same "ID" was participating 2022, the "DepressedElection" should tell the value of the variable "gloomy" for 2021 for that specific person/"ID". If that same person, with the same "ID" was participating 2023, the "DepressedElection" should tell the value of the variable "gloomy" for 2021 for that specific person/"ID".
If the person was not participating in the survey for an election year, the years after until the next election should be missing variables. For instance, if If that same person, with the same "ID" was participating 2022 and 2023, but not 2021, the "DepressedElection" should be missing for the years 2022 and 2023 for that specific person/"ID". The years before 2010 should also be missing.
Sorry for the long question. Thank you for your help!
Best regards,
Klara
I'm not the best at stata and need help how to code a new variable in my panel dataset (2007-2023). The purpose of the variable is to tell me how depressed/gloomy someone was feeling during a the latest election year.
I have a variable for "ID" (each person), "year" (some people are not participating every year) and "gloomy" (scale from 1-7). The election years were 2010, 2012, 2017, 2021.
I want the new variable "DepressedElection" to tell me the value of "gloomy" for the latest election year, and if the person was not participating in the survey in the lastest election year I want the "DepressedElection" to be missing.
So to be concrete: If the person with ID "..." was participating in the survey year 2010, the "DepressedElection" should tell the value of the variable "gloomy" for 2010. If that same person, with the same "ID" was participating 2011, the "DepressedElection" should tell the value of the variable "gloomy" for 2010 for that specific person/"ID". If that same person, with the same "ID" was participating 2012, the "DepressedElection" should tell the value of the variable "gloomy" for 2012, since there was a new election in 2012, for that specific person/"ID". If that same person, with the same "ID" was participating 2013, the "DepressedElection" should tell the value of the variable "gloomy" for 2012 for that specific person/"ID". If that same person, with the same "ID" was participating 2014, the "DepressedElection" should tell the value of the variable "gloomy" for 2012 for that specific person/"ID". If that same person, with the same "ID" was participating 2015, the "DepressedElection" should tell the value of the variable "gloomy" for 2012 for that specific person/"ID". If that same person, with the same "ID" was participating 2016, the "DepressedElection" should tell the value of the variable "gloomy" for 2012 for that specific person/"ID". If that same person, with the same "ID" was participating 2017, the "DepressedElection" should tell the value of the variable "gloomy" for 2017 for that specific person/"ID", since there was a new election. If that same person, with the same "ID" was participating 2018, the "DepressedElection" should tell the value of the variable "gloomy" for 2017 for that specific person/"ID". If that same person, with the same "ID" was participating 2019, the "DepressedElection" should tell the value of the variable "gloomy" for 2017 for that specific person/"ID". If that same person, with the same "ID" was participating 2020, the "DepressedElection" should tell the value of the variable "gloomy" for 2017 for that specific person/"ID". If that same person, with the same "ID" was participating 2021, the "DepressedElection" should tell the value of the variable "gloomy" for 2021 for that specific person/"ID", since it was a year with an election. If that same person, with the same "ID" was participating 2022, the "DepressedElection" should tell the value of the variable "gloomy" for 2021 for that specific person/"ID". If that same person, with the same "ID" was participating 2023, the "DepressedElection" should tell the value of the variable "gloomy" for 2021 for that specific person/"ID".
If the person was not participating in the survey for an election year, the years after until the next election should be missing variables. For instance, if If that same person, with the same "ID" was participating 2022 and 2023, but not 2021, the "DepressedElection" should be missing for the years 2022 and 2023 for that specific person/"ID". The years before 2010 should also be missing.
Sorry for the long question. Thank you for your help!
Best regards,
Klara
Comment