Hi everyone,

I have a question about how to randomly sample data in Stata according to specific groups. Below is my data structure:

I want to randomly sample 1/3 of the data according to the ids. For instance, if I randomly choose id 3, then I'll keep all the observations of id3, in my case 4 obs.
Similarly, if I randomly choose id 1, then I want to keep all the observations of id 1.

Does anyone know how to realize this in Stata?


Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte id str9 date
1 "2022/1/2" 
1 "2022/1/8" 
1 "2022/1/9" 
1 "2022/1/12"
1 "2022/1/13"
1 "2022/1/14"
2 "2022/1/3" 
2 "2022/1/6" 
2 "2022/2/1" 
2 "2022/2/8" 
2 "2022/2/11"
3 "2022/2/6" 
3 "2022/2/9" 
3 "2022/2/13"
3 "2022/2/15"
end
Thanks a lot!