Dear All,

I am new to loop. Is there a way I can replace the missing values with the number 1 each time they are selected in a random selection. I was thinking if we can use the command "randomselect" with loop. The percentage of selection is 4% of the total for each year. The unique id is identification (which will be unique for each person) So for each year i will have to randomly select 4% and assign it a number 1. It is possble that the same person might be selected twice. How can I do that using loop and randomselect. My data is in long format

Any help would be useful since I am quite new to looping in STATA

A sample of my dataset Some variables have been renamed since these are highly confidential data


copy starting from the next line ------- ---------------
Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input double empid float var1 str14 sublevel float year
2001001 1 "A" 2007
2001001 . "A" 2008
2001001 . "A" 2009
2001001 . "A" 2010
2001001 . "A" 2011
2001001 . "A" 2012
2001001 . "A" 2013
2001001 . "A" 2014
2001001 . "A" 2015
2001001 . "A" 2016
2001001 . "A" 2017
2001001 . "A" 2018
2001001 . "A" 2019
2001001 . "A" 2020
2001001 . "A" 2021
2001001 . "A" 2022
2001001 . "A" 2023
2001001 . "A" 2024
2001001 . "A" 2025
2001001 . "A" 2026
2001004 1 "A" 2007
2001004 . "A" 2008
2001004 . "A" 2009
2001004 . "A" 2010
2001004 . "A" 2011
2001004 . "A" 2012
2001004 . "A" 2013
2001004 . "A" 2014
2001004 . "A" 2015
2001004 . "A" 2016
2001004 . "A" 2017
2001004 . "A" 2018
2001004 . "A" 2019
2001004 . "A" 2020
2001004 . "A" 2021
2001004 . "A" 2022
2001004 . "A" 2023
2001004 . "A" 2024
2001004 . "A" 2025
2001004 . "A" 2026
2001008 1 "A" 2007
2001008 . "A" 2008
2001008 . "A" 2009
2001008 . "A" 2010
2001008 . "A" 2011
2001008 . "A" 2012
2001008 . "A" 2013
2001008 . "A" 2014
2001008 . "A" 2015
2001008 . "A" 2016
2001008 . "A" 2017
2001008 . "A" 2018
2001008 . "A" 2019
2001008 . "A" 2020
2001008 . "A" 2021
2001008 . "A" 2022
2001008 . "A" 2023
2001008 . "A" 2024
2001008 . "A" 2025
2001008 . "A" 2026
2001009 1 "A" 2007
2001009 . "A" 2008
2001009 . "A" 2009
2001009 . "A" 2010
2001009 . "A" 2011
2001009 . "A" 2012
2001009 . "A" 2013
2001009 . "A" 2014
2001009 . "A" 2015
2001009 . "A" 2016
2001009 . "A" 2017
2001009 . "A" 2018
2001009 . "A" 2019
2001009 . "A" 2020
2001009 . "A" 2021
2001009 . "A" 2022
2001009 . "A" 2023
2001009 . "A" 2024
2001009 . "A" 2025
2001009 . "A" 2026
2001010 1 "A" 2007
2001010 . "A" 2008
2001010 . "A" 2009
2001010 . "A" 2010
2001010 . "A" 2011
2001010 . "A" 2012
2001010 . "A" 2013
2001010 . "A" 2014
2001010 . "A" 2015
2001010 . "A" 2016
2001010 . "A" 2017
2001010 . "A" 2018
2001010 . "A" 2019
2001010 . "A" 2020
2001010 . "A" 2021
2001010 . "A" 2022
2001010 . "A" 2023
2001010 . "A" 2024
2001010 . "A" 2025
2001010 . "A" 2026
end
label values var1 positionl
copy up to and including the previous line -- ---------------