title: How to output SAS IML values for printing
body: <p>I would like to take values that are calculated in IML to be used in SAS' printing functionality %PRNTINIT.  This functionality is used to provide weekly reports from a database that can get updated.</p>
  
  <p>I have some legacy code that uses <code>proc sql</code> to declare macro-type values that are called upon later, e.g.:</p>
  
  <h2>Declaring variables: tot1-tot4</h2>
  
  <pre><code>*Get total number of subjects in each group in macro variable;
  proc sort data = avg3; by description; run;
  proc sql noprint;
  select _freq_
  into: tot1-:tot4
  from avg3;
  quit;
  </code></pre>
  
  <h2>Calling variables tot1-tot4 for printing</h2>
  
  <pre><code>%print(column = 1, style = bold, just = center, lines = bottom:none);
  %print("(N= &amp;tot1)", column = 2, just = center, lines = bottom:none);
  %print("(N= &amp;tot2)", column = 3, just = center, lines = bottom:none);
  %print("(N= &amp;tot3)", column = 4,  just = center, lines = bottom:none);
  %print("(N= &amp;tot4 )", column = 5,  just = center, lines = bottom:none);
  %print(column = 6, just = center, lines = bottom:none);
  </code></pre>
  
  <p>I would like to be able to call values from IML similarly, if possible.</p>
  
  <hr>
  
  <p>Example data:</p>
  
  <pre><code>data test ;
  input age  type  gender $;
  cards;
  1 1 m
  1 1 m
  1 1 m
  1 1 f
  1 1 f
  1 2 f
  2 1 m
  2 1 f
  2 2 m
  2 2 m
  2 2 m
  2 2 m
  2 2 m
  2 2 f
  2 2 f
  2 2 f
  ;
  
  
  
  
  proc freq data = test;
  tables type*age /  chisq norow nocol nopercent outexpect out=out1 ;
  tables type*gender / chisq norow nocol nopercent outexpect out=out2 ;
  run;
  
  
  options missing=" ";
  
  proc iml;
  
  reset print; 
  
  use out2;
  read all var {count} into count;
  
  type1 = count[1:2] ;
  type2 = count[3:4] ;
  
  tab = type1 || type2 ;
  
  cols = tab[+,] ;
  rows = tab[,+] ;
  tot  = sum(tab) ;
  
  perc = round(cols / tot, .01) ;
  
  cell_perc = round(tab / (cols//cols) , .01) ;
  
  
  expect = (rows * cols) / tot ;
  chi_1 = sum((tab - expect)##2/expect) ;
  p_chi_1 = 1-CDF('CHISQUARE',chi_1, ((ncol(tab)-1)*(nrow(tab)-1)));
  
  
  
  print tab p_chi_1 perc cell_perc;
  
  
  
  out_sex = tab || (. // p_chi_1);
  
  print out_sex;
  
  print out_sex[colname={"1","2"} 
                rowname={"f" "m" "p-value"}
                label="Table of Type by Gender"];
  
  
  
  
  call symput(t1_sum, cols[1,1]) ;
  
  %let t2_sum = put(cols[1,2]) ;
  %let t1_per = perc[1,1] ;
  %let t2_per = perc[1,2] ; 
  
  
  
  %let t1_f = tab[1,1] ;
  %let t1_m = tab[2,1] ;
  %let t2_f = tab[1,2] ;
  %let t2_m = tab[2,2] ;
  
  %let t1_f_p = cell_perc[1,1] ;
  %let t1_m_p = cell_perc[2,1] ;
  %let t2_f_p = cell_perc[1,2] ;
  %let t2_m_p = cell_perc[2,2] ;
  
  
  
  %let p_val = p_chi_1 ;
  
  
  *****  is it possible to list output values here for use in table building ???   ;
  *  like:    %let t1_f = tab[1,1]
              %let t2_f = tab[2,1]   etc...  ;
  
  quit;
  </code></pre>
  
  <hr>
  
  <p>So  I would like to declare a print statement like the following:</p>
  
  <pre><code>%print( "(N=&amp;tab[1,1], column = 1, just=center, lines = bottom:none);
  %print( "(N=&amp;tab[1,2], column = 2, just=center, lines = bottom:none);
  etc...
  </code></pre>
  
  <p>Any help on this is greatly appreciated...</p>
  
  <hr>
  
  <h2>Update: Unable to extract declared macro values out of IML</h2>
  
  <p>I have been able to calculate the correct values and format the table successfully.</p>
  
  <p><em>However,</em> I am unable to extract the values for use in the print macro.</p>
  
  <p>I have created some matrices and calculated values in IML, but when I try to declare macro variables for use later, the only thing that is returned is the literal value that I declared the variable to be... e.g.:</p>
  
  <p><img src="https://i.stack.imgur.com/0XJym.gif" alt="table with missing IML values"></p>
  
  <p>and</p>
  
  <p><img src="https://i.stack.imgur.com/7XoOO.gif" alt="another table with missing IML values"></p>
  
  <p>You can see in the table what I <strong>want</strong> the numbers to be, but have thus far been unsuccessful.  I have tried using <code>%let</code>, <code>put</code>, <code>symput</code> , and <code>symputx</code> without success.</p>
  
  <pre><code>call symput(t1_sum, cols[1,1]) ;
  
  %let t2_sum = put(cols[1,2]) ;
  %let t1_per = perc[1,1] ;
  %let t2_per = perc[1,2] ; 
  
  
  
  %let t1_f = tab[1,1] ;
  %let t1_m = tab[2,1] ;
  %let t2_f = tab[1,2] ;
  %let t2_m = tab[2,2] ;
  
  %let t1_f_p = cell_perc[1,1] ;
  %let t1_m_p = cell_perc[2,1] ;
  %let t2_f_p = cell_perc[1,2] ;
  %let t2_m_p = cell_perc[2,2] ;
  </code></pre>
  
  <p>Blerg...</p>