合并SAS中的宏表

时间:2018-11-29 17:25:15

标签: merge macros sas

我是SAS的初学者,所以我不熟悉语法。我有两个使用宏创建的数据集。 (宏:https://gist.github.com/statgeek/2f27939fd72d1dd7d8c8669cd39d7e67

DATA test1; 
     set sashelp.class;
     if prxmatch('m/M/oi', sex);
     female=ifn( sex='F',1,0);
RUN; 

%table_char(test1, height weight age, sex, female, test1_table_char); 

DATA test2; 
     set sashelp.class;
     if prxmatch('m/F/oi', sex);
     female=ifn( sex='F',1,0);
RUN; 

%table_char(test2, height weight age, sex, female, test2_table_char);

所需的输出:

               Male       Female 
Height 
  Count 
  Mean
  Median
   .
   .
Weight
Count 
  Mean
  Median
   .
   .
Sex
  M
  F
Etc

我想将两个宏表合并在一起,并按名称将%table_char创建。我应该怎么称呼这两个表以便合并?

DATA final_merge; 
     merge test1_table_char   test2_table_char;
     by NAME;
RUN;

2 个答案:

答案 0 :(得分:0)

看起来您需要做的就是追加数据集。

 data final;
   set test1 test2;
 run;

您无需拆分和合并数据集,只需完成操作即可。

  DATA final; 
 set sashelp.class;
 female=ifn( sex='F',1,0);
RUN;

如果要合并,请对数据集进行排序,然后再合并数据集

 proc sort data =test1;
 by your_variable;
  run;

 proc sort data =test2;
 by your_variable;
  run;

  data final;
  merge test1 test2
   by your_variable;
  run;

答案 1 :(得分:0)

按名称合并或合并test1和test2:

proc sort data=work.test1;                                                                                                                                                                                                                                      
   by name;                                                                                                                                                                                                                                                     
run;                                                                                                                                                                                                                                                            

proc sort data=work.test2;                                                                                                                                                                                                                                      
   by name;                                                                                                                                                                                                                                                     
run;                                                                                                                                                                                                                                                            

data work.test;                                                                                                                                                                                                                                                 
   merge work.test1 work.test2;                                                                                                                                                                                                                                 
   by name;                                                                                                                                                                                                                                                     
run; 

产生此结果:

Name       Sex    Age    Height    Weight    female

Alfred      M      14     69.0      112.5       0
Alice       F      13     56.5       84.0       1
Barbara     F      13     65.3       98.0       1
Carol       F      14     62.8      102.5       1
Henry       M      14     63.5      102.5       0
James       M      12     57.3       83.0       0
Jane        F      12     59.8       84.5       1
Janet       F      15     62.5      112.5       1
Jeffrey     M      13     62.5       84.0       0
John        M      12     59.0       99.5       0
Joyce       F      11     51.3       50.5       1
Judy        F      14     64.3       90.0       1
Louise      F      12     56.3       77.0       1
Mary        F      15     66.5      112.0       1
Philip      M      16     72.0      150.0       0
Robert      M      12     64.8      128.0       0
Ronald      M      15     67.0      133.0       0
Thomas      M      11     57.5       85.0       0
William     M      15     66.5      112.0       0

运行具有合并输出的宏:

%table_char(test, height weight age, sex, female, test_table_char); 

产生以下结果:

categorical        value

Sex
F                      9(47.4%)
M                     10(52.6%)
Height
Count(Missing)     19(0)
Mean (SD)           62.3(5.1)
Median (IQR)        62.8(57.5 - 66.5
Range               51.3 - 72.0
90th Percentile     69.0
Weight
Count(Missing)     19(0)
Mean (SD)          100.0(22.8)
Median (IQR)        99.5(84.0 - 112.
Range               50.5 - 150.0
90th Percentile    133.0
Age
Count(Missing)     19(0)
Mean (SD)           13.3(1.5)
Median (IQR)        13.0(12.0 - 15.0
Range               11.0 - 16.0
90th Percentile     15.0
Female                 9(47.4%)