Hello SAS community, First, I want to say how grateful I am to find this discussion board. I think it is so valuable there is a community of people who are willing to help one another with their SAS programming questions and I want to thank anyone in advance who comments and replies. I hope one day I am a good enough SAS programmer to return the favor and help others. My goal is to take a dataset of multiple variables and find how many times unique combinations of pairs of variables exist in the dataset. First, I created a fake dataset with three variables called y1, y2, y3. Then, I have a macro that uses proc freq on each pair of combinations. The last step is to combine the frequencies (and convert it to a fraction) into one data set with the original variables. Here is my SAS program: data test_data; do i = 1 to 10; cod = i; call streaminit(123); u1 = rand("Uniform"); y1 = ceil(5*u1); call streaminit(456); u2 = rand("Uniform"); y2 = ceil(5*u2); call streaminit(789); u3 = rand("Uniform"); y3 = ceil(5*u3); output; end; drop u1 u2 u3 i; run; /*proc print data = test_data; run;*/ %macro pairedfreqs(dataset, order, k, vars); proc freq data = &dataset order=data noprint; tables y1*y2/ out = pfreq1 list nopct nocum; run; proc freq data = &dataset order=data noprint; tables y1*y3/ out = pfreq2 list nopct nocum; run; proc freq data= &dataset order=data noprint; tables y2*y3/ out = pfreq3 list nopct nocum; run; %do i=1 %to &k; proc sort data=&dataset; by %scan(&vars,&i); run; proc sort data=pfreq&i; by %scan(&vars,&i); run; data f&i(drop=count percent); merge &dataset pfreq&i; by %scan(&vars,&i); pfreq&i=percent/100; run; proc sort; by ℴ run; %end; data all_freqs; merge %do i=1 %to &k; f&i %end;; by ℴ run; %mend; %pairedfreqs(dataset=test_data,order=cod,k = 3, vars= y1 y2 y3); The problem arises with my do-loop, specifically where I create dataset "f2" because there is no variable y2 in this dataset. So the final data set of interest, all_freqs, has missing values for pfreq2 even though the actual data set pfreq2 does exist and is correct. I have searched the internet and these discussion boards, and I have tried many different things but I am stuck. Any advice on fixing what I have so the correct data set results is appreciated. The second step, or my second question, is then how to edit my macro so that the idea behind this procedure can extend to datasets with any number of variables. Meaning, change the macro contents so that if someone has a dataset with 5 variables (and is using the macro with the argument vars = var1 var2 var3 var4 var5), the macro will find all the combinations of pairs of two variables, find the frequencies (convert to a decimal), and then merge together in a final data set. If further clarification is needed, let me know. Thanks!!
... View more