【问题标题】:SAS put variable format in titleSAS将变量格式放在标题中
【发布时间】:2014-10-17 21:21:11
【问题描述】:

我有一个名为 agegroup 的变量,它有很多类别,例如:1="0-5" 2="6-10" 等。

如果我创建一个宏来打印agegroup 的数据,并且想在标题中使用年龄组格式,我该怎么做?

%macro ageprint(agegrp=1);
 proc print data=age;
 title "Age group &agegrp";
 run;
%mend;

如果我运行此宏,我希望将标题打印为“Age group 0-5”而不是“age group 1”。

谁有提示?

谢谢!

【问题讨论】:

    标签: sas sas-macro


    【解决方案1】:

    您也可以使用#byval(age) 选项,它采用标题中的格式化值:

    proc format ;
    value grpformat
            0 - 12 = '0 - 12'
            12 - 14 = '12 -  14'
            other = 'other'
                ;
    run;
    
    proc sort data=sashelp.class out=class; by age; run;
    
    proc print data=class;
    by age;
    format age grpformat.;
    title "Age Group #byval(age)";
    run;
    

    如果您需要一个宏来控制输出的去向,您仍然可以使用相同的方法,例如:

    %macro ageprint(agegrp=1);
     proc print data=age;
     by agegrp;
     title "Age group #byval(agegrp)";
     run;
     %mend;
    

    【讨论】:

    • 嗨,里斯,感谢您的回复!我在我的宏中尝试了你的解决方案,但 '#byval' 被打印出来了。不确定问题出在哪里。
    • 如果您的代码也很难分辨,您还必须在 proc 中包含 by。
    【解决方案2】:

    这样的?

    proc format ;
    value grpformat
            0 - 5 = '0 - 5'
            6 - 10 = '6 -  10'
            other = 'other'
                ;
    run;
    
    %macro ageprint(agegrp);
     proc print data=age;
      where agegrp=&agegrp;
     title "Age group %sysfunc(putn(&agegrp, grpformat.))";
     run;
    %mend;
    
    %ageprint(agegrp=2);
    %ageprint(agegrp=8);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多