sas - Selecting a random column from a group of columns of interest -
i trying come code select random column group of columns on interest. group of columns change depending on values in columns each observation. each observation subject.
let me explain more clear:
i have 8 columns, names v1-v8. each column has 3 potential responses ('small','medium','high'). due circumstances in our project, need "combine" information 1 column.
key factor 1: want columns per subject he/she selected 'high' (lots of combinations here). refer when columns of interest changes per subject.
key factor 2: once have identified columns 'high' selected subject, select 1 of columns @ random.
at end, need new variable (new_v) values v1-v8 (not 'small','medium','high') indicating column selected each subject.
any advice great. have tried arrays , macro variables can seem tackle right way.
this method uses macro variables , loop. there 3 main steps: first, find variables "high." second, select random value 1 number of variables "high." third, pick variable , call selected_var.
data temp; input subject $ v1 $ v2 $ v3 $ v4 $ v5 $ v6 $ v7 $ v8 $; datalines; 1 high medium small high medium small high medium 2 medium small high medium small high medium high 3 small high high medium small high medium high 4 medium medium high medium small small medium medium 5 medium medium high small small high medium small 6 small small high medium small high high high 7 small small small small small small small small 8 high high high high high high high high ; run; %let vars = v1 v2 v3 v4 v5 v6 v7 v8; %macro find_vars; data temp2; set temp; /*find possible variables*/ format possible_vars $20.; %do = 1 %to %sysfunc(countw(&vars.)); %let this_var = %scan(&vars., &i.); if &this_var. = "high" possible_vars = cats(possible_vars, "&this_var."); %end; /*create random integer between 1 , number of variables select from*/ rand = 1 + floor((length(possible_vars) / 2) * rand("uniform")); /*pick one!*/ selected_var = substr(possible_vars, (rand * 2 - 1), 2); run; %mend find_vars; %find_vars;