【问题标题】:sas data _null_, formats & put statement outputting extra line breaksas 数据 _null_、格式和 put 语句输出额外的换行符
【发布时间】:2015-09-09 22:52:21
【问题描述】:

我正在尝试创建一个类似于本文中创建的报告表: http://www.lexjansen.com/wuss/2004/data_presentation/c_dp_creating_custom_tables_.pdf

但是,由于某种原因,表中的分类(即格式化)变量在每个子类别之后输出了额外的一行。

论文中使用的代码:

data _null_;
 set Table_DS;
 by entry;

 file out print header=hdr linesleft=remain;

 * --- Report Body;
 if first.entry then put /@&COL1 entry entry.;
 else if entry=3 then put @&COL1 +3 gender gender.;
 else if entry=4 then put @&COL1 +3 race race.; 
 put @&COL2 statc statc.
 @&COL3 p1
 @&COL4 p2
 @&COL5 p3;

 * --- Footer;
 *Note: Cutoff value depends on the number;
 * of lines required for the footer;
 if last or remain < 9 then link ftr;
 return;
 * ----- PUT HEADER;
 hdr:
 ...Insert titles...
 ...Insert column headers...
 return;
 * ----- PUT FOOTER;
 ftr:
 ...Insert footnotes...
 ...Begin a new page...
 return;
run; 

我的代码:

data _null_;
 set Table_DS end=last;
 by entry;
 file print header=hdr linesleft=remain;
 * --- Report Body;
 if first.entry then put /@ &COL1 entry entry.;
 if entry=1 then put @ &COL1 +3 lineno sex.;
 if entry=2 then put @ &COL1 +3 lineno race.; 
 if entry=3 then put @ &COL1 +3 lineno ethnic.;
 if entry=6 then put @ &COL1 +3 lineno creatcat.;
 if entry=7 then put @ &COL1 +3 lineno admitdx.;
 put @ &COL2 statc statc.
     @ &COL3 p1
     @ &COL4 p2
     @ &COL5 p3;
 if last or remain < 4 then link ftr;
 return;
 hdr: 
  put
 @1 'Table 4: Demographic and Baseline Characteristics'
 /@1 '(ITT analysis set)'
 /@1 '________________________________________________________________________________________________'
 /@1 'Subject Stratum: All Strata'
 /@56 'Group A       Group B       Total'
 /@56 '(N=294)       (N=285)       (N=579)'
 /@1 '________________________________________________________________________________________________';
return;
 ftr:
  put
   @1 '________________________________________________________________________________________________'
   /@1 "Using &ds data set from &deldate";
 return;
 return;
run; 

从我的代码中选择的输出:

Table 4: Demographic and Baseline Characteristics
(ITT analysis set)
________________________________________________________________________________________________
Subject Stratum: All Strata
                                                       Group A       Group B    Total
                                                       (N=294)       (N=285)       (N=579)
________________________________________________________________________________________________

       Sex                          

                                         N             294           285           579
          Female
                                         n (%)         78 ( 26.5%)   80 ( 28.1%)   158 ( 27.3%)

       Race                         

                                         N             293           280           573
          White                         
                                         n (%)         289 ( 98.6%)  277 ( 98.9%)  566 ( 98.8%)
          Black or African American     
                                         n (%)         2 (  0.7%)    0 (N/A)       2 (  0.3%)
          Asian                         
                                         n (%)         1 (  0.3%)    0 (N/A)       1 (  0.2%)
          American Indian/Alaskan Native
                                         n (%)         0 (N/A)       1 (  0.4%)    1 (  0.2%)
          Other                         
                                         n (%)         1 (  0.3%)    2 (  0.7%)    3 (  0.5%)

       Ethnicity                    

                                         N             293           281           574
          Not Hispanic/Latino
                                         n (%)         284 ( 96.9%)  266 ( 94.7%)  550 ( 95.8%)
          Hispanic/Latino    
                                         n (%)         9 (  3.1%)    15 (  5.3%)   24 (  4.2%)

       Age (yrs)                    
                                         N             294           285           579
                                         Mean          61.5          62.1          61.8
                                         SD            9.2           9.2           9.2
                                         Median        61.0          61.0          61.0
                                         Minimum       34.0          36.0          34.0
                                         Maximum       81.0          85.0          85.0

       BMI (kg/m^2)                 
                                         N             290           280           570
                                         Mean          28.8          28.9          28.9
                                         SD            4.8           4.7           4.7
                                         Median        28.0          28.4          28.2
                                         Minimum       18.2          18.7          18.2
                                         Maximum       46.5          48.2          48.2

我查看了整篇论文并比较了我们的代码,但我无法弄清楚为什么格式化的分类变量将统计数据按到下一行,而连续的统计数据在打印时没有任何额外的行。

这里是示例数据:

P1  lineno  statc   P2  P3  entry
294 -1  1   285 579 1
78 ( 26.5%) 1   7   80 ( 28.1%) 158 ( 27.3%)    1
293 -1  1   280 573 2
289 ( 98.6%)    1   7   277 ( 98.9%)    566 ( 98.8%)    2
2 ( 0.7%)   2   7   0 (N/A) 2 ( 0.3%)   2
1 ( 0.3%)   3   7   0 (N/A) 1 ( 0.2%)   2
0 (N/A) 4   7   1 ( 0.4%)   1 ( 0.2%)   2
1 ( 0.3%)   5   7   2 ( 0.7%)   3 ( 0.5%)   2
293 -1  1   281 574 3
284 ( 96.9%)    0   7   266 ( 94.7%)    550 ( 95.8%)    3
9 ( 3.1%)   1   7   15 ( 5.3%)  24 ( 4.2%)  3
294 .   1   285 579 4
61.5    .   2   62.1    61.8    4
9.2 .   3   9.2 9.2 4
61  .   4   61  61  4
34  .   5   36  34  4
81  .   6   85  85  4
290 .   1   280 570 5
28.8    .   2   28.9    28.9    5
4.8 .   3   4.7 4.7 5
28  .   4   28.4    28.2    5
18.2    .   5   18.7    18.2    5
46.5    .   6   48.2    48.2    5

【问题讨论】:

  • 如果你在这个语句的末尾加上一个@ 符号(以防止 put 语句以换行符结束)会发生什么:@ &amp;COL5 p3 @;?
  • 顺便说一句:这是一种用于学习 SAS 报告基础知识的有趣方法,但就目前而言,我不会在生产中使用它。 PROC REPORT 能够生成相同的表,或者足够接近,而定制工作要少得多,如果你确实需要一些定制的东西,你可以通过使用 proc template 表甚至只是将上面的宏宏化来做得比这更好.
  • @RobertPenr​​idge 似乎已经将“N”行向下移动了 1 行,因此所有 n(%) 也向下移动了 1 行。此外,除了最后一行“Maximum”之外,连续的 var 描述都消失了。我将继续使用指针,但我可能会听取 Joe 的建议并考虑使用 proc 报告。感谢您的帮助!

标签: sas


【解决方案1】:

你的问题是通过分析上面的代码的不同发现的。凭经验做到这一点有点困难(因为你不提供样本数据,所以我不能 100% 确定数据集的样子),但尽管如此:

您的代码:

if first.entry then put /@ &COL1 entry entry.;
 if entry=1 then put @ &COL1 +3 lineno sex.;
 if entry=2 then put @ &COL1 +3 lineno race.; 
 if entry=3 then put @ &COL1 +3 lineno ethnic.;
 if entry=6 then put @ &COL1 +3 lineno creatcat.;
 if entry=7 then put @ &COL1 +3 lineno admitdx.;

论文中的原始代码:

 if first.entry then put /@&COL1 entry entry.;
 else if entry=3 then put @&COL1 +3 gender gender.;
 else if entry=4 then put @&COL1 +3 race race.; 

在第一行,first.entry,它放置条目(Sex 等),然后处理另一行if entry=1 then put @ &amp;COL1 +3 lineno sex.; 我假设 lineno 可能在那些上丢失行(或者是 0 或其他被 sex. 格式设置为缺失的东西)。

在原始论文中,从未针对 first.entry 行执行该操作,因为它位于 else 块中。

【讨论】:

  • 不幸的是,在 lineno 语句中添加适当的“else if”会产生与仅使用“if”相同的结果。
猜你喜欢
  • 2016-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-27
相关资源
最近更新 更多