1

我想将在 IML 中计算的值用于 SAS 的打印功能 %PRNTINIT。此功能用于从可以更新的数据库中提供每周报告。

我有一些遗留代码用于proc sql声明稍后调用的宏类型值,例如:

声明变量:tot1-tot4

*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;

调用变量 tot1-tot4 进行打印

%print(column = 1, style = bold, just = center, lines = bottom:none);
%print("(N= &tot1)", column = 2, just = center, lines = bottom:none);
%print("(N= &tot2)", column = 3, just = center, lines = bottom:none);
%print("(N= &tot3)", column = 4,  just = center, lines = bottom:none);
%print("(N= &tot4 )", column = 5,  just = center, lines = bottom:none);
%print(column = 6, just = center, lines = bottom:none);

如果可能的话,我希望能够类似地从 IML 调用值。


示例数据:

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;

所以我想声明一个打印语句,如下所示:

%print( "(N=&tab[1,1], column = 1, just=center, lines = bottom:none);
%print( "(N=&tab[1,2], column = 2, just=center, lines = bottom:none);
etc...

非常感谢您对此的任何帮助...


更新:无法从 IML 中提取声明的宏值

我已经能够计算出正确的值并成功格式化表格。

但是,我无法提取用于打印宏的值。

我在 IML 中创建了一些矩阵和计算值,但是当我尝试声明宏变量以供以后使用时,唯一返回的是我将变量声明为的文字值......例如:

缺少 IML 值的表

另一个缺少 IML 值的表

您可以在表格中看到我想要的数字,但到目前为止还没有成功。我尝试使用%let, put, symput, 并symputx没有成功。

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] ;

布雷格...

4

2 回答 2

0

SAS/IML 矩阵必须全部为数字或全部为字符,这就是您的尝试无效的原因。但是,SAS/IML PRINT 语句有几个选项可以让您标记列和行并应用格式。请参阅http://blogs.sas.com/content/iml/2011/08/01/options-for-printing-a-matrix/ 连同全局 SAS OPTIONS 语句,我认为您可以获得所需的输出。

1)使用全局语句

options missing=" ";

告诉 SAS 将缺失值打印为空白。

2)您的目标是将一列附加到 2x2 TAB 矩阵。您可以对没有数据的行使用(数字)缺失值:

   out_age = tab || (. // p_chi_1);

3) 您现在可以打印这个 2x3 表格,并使用 COLNAME= 和 ROWNAME= 选项来显示行标题:

print out_age[rowname={"1","2"} 
              colname={"f" "m" "p-value"}
              label="Table of Type by Gender"];

如果您真的想使用旧式宏,可以使用 SYMPUTX 语句将值从 SAS/IML 复制到宏变量中,如本文所示:http: //blogs.sas.com/content/iml/2011 /10/17/does-symput-work-in-iml/

于 2012-12-29T19:07:30.573 回答
0

经过大量搜索,并在 SO 上找到了一些帮助,我能够将答案拼凑在一起。

  1. 输出值必须是字符串
  2. 宏变量的名称必须用引号引起来
  3. 调用必须是symputx为了修剪额外的空白(与 相比symput

将 IML 值提取到宏变量的代码

call symputx('t1_sum', char(cols[1,1])) ;
call symputx('t2_sum', char(cols[1,2])) ;

call symputx('t1_per', char(perc[1,1])) ;
call symputx('t2_per', char(perc[1,2])) ;

call symputx('t1_f' , char(tab[1,1])) ;
call symputx('t1_m' , char(tab[2,1])) ;
call symputx('t2_f' , char(tab[1,2])) ;
call symputx('t2_m' , char(tab[2,2])) ;

call symputx('t1_f_p' , char(cell_perc[1,1])) ;
call symputx('t1_m_p' , char(cell_perc[2,1])) ;
call symputx('t2_f_p' , char(cell_perc[1,2])) ;
call symputx('t2_m_p' , char(cell_perc[2,2])) ;

call symputx('p_val' , char(round(p_chi_1, .001))) ;

用于使用旧 SAS %PRNTINIT 宏构建表的部分代码

...
%print("Female",            column = 1,  just = center  );
%print("&t1_f (&t1_f_p)",   column = 2,  just = center  );
%print("&t2_f (&t2_f_p)",   column = 3,  just = center  );
%print(,                    column = 4,  just = center  );
%print(proc = newrow);

%print("Male",              column = 1,  just = center  );
%print("&t1_m (&t1_m_p)",   column = 2,  just = center  );
%print("&t2_m (&t2_m_p)",   column = 3,  just = center  );
%print("&p_val",            column = 4,  just = center  );
...

期望的结果:

带有正确数字的表格详细信息

带有正确数字的附加表格详细信息

于 2013-01-02T19:55:38.000 回答