问题
I have been facing some difficulties in managing dietary data regarding cereal consumption. The question allows participants to select as many cereal types that they eat (among 300+ choices). I would like to collapse this into just two answers through random selection. The data structure is posing problems. I will outline my problem below…
1.) Structure of the data now: Respondents are allowed to select all cereals that they “usually” eat. The data looks like (1 is replaced with a code for the cereal)…
Cereal 1 Cereal 2 Cereal 3 Cereal 4 Cereal 5
1 0 1 1 0
2.) I will need to collapse these choices into just two, even when there are more than two cereals selected. What I am thinking is that I would first collapse these choices into as many as each respondent selected. I want the data to be changed to the structure below…
Choice 1 Choice 2 Choice 3 Choice 4 Choice 5
Cereal 1 Cereal 3 Cereal 4 . .
3.) I would then want to randomly select two of these (since we cannot distinguish order of selection) to be displayed like the data below…
Choice 1 Choice 2
Cereal 1 (randomly selected) Cereal 4 (Randomly selected)
Does anyone have an idea of how I might accomplish this? I have been staring at my code for some time and can’t quite seem to figure this out. Please let me know if further explanation is needed.
回答1:
Assumptions:
- Cereal variables are numeric
- 0 is missing and otherwise the values for a cereal are larger than 0
- Cereals have a unique code so variable names are not important
Everyone has selected at least two cereals.
data want; set have; *set random seed to get the same 'random' results; call streaminit(999); *declare an array to simplify code; array cereal(*) cereal1-cereal300; *sort array so that cereals come first and 0s last; call sort(of cereal(*)); *find the first 0; *this tells you how many cereals are selected; numCereals= whichn(0, of cereal(*)) - 1; *get a random number between those two numbers; rand1 = rand('integer', 1, numCereals); *ensure that your second random number isn't the same as the first random number; do until(rand2 ne rand1); rand2 = rand('integer', 1, numCereals); end; *get the values at those random index; randomCereal1 = cereal(rand1); randomCereal2 = cereal(rand2); run;
There is untested.
来源:https://stackoverflow.com/questions/61525204/transforming-expansive-check-all-that-apply-type-questions-into-random-selecti