我想将在IML中计算的值用于SAS'打印功能%PRNTINIT。此功能用于提供可以更新的数据库的每周报告。
我有一些遗留代码使用proc sql
来声明稍后调用的宏类型值,例如:
*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;
%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中创建了一些矩阵和计算值,但是当我尝试声明宏变量以供以后使用时,唯一返回的是我声明变量的文字值...例如:
和
你可以在表格中看到我想要的数字,但到目前为止还没有成功。我尝试过使用%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] ;
... Blerg
答案 0 :(得分: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/
答案 1 :(得分:0)
在多搜索并找到一些帮助here on SO后,我能够将答案拼凑在一起。
symputx
以修剪额外的空格(与symput
相比时)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))) ;
...
%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 );
...
期望的结果: