【问题标题】:finding max of many columns using proc sql statement使用 proc sql 语句查找多列的最大值
【发布时间】:2016-10-05 22:05:58
【问题描述】:

我正在尝试在 SAS 中编写一个 PROC SQL 查询,以确定以特定字母开头的最多列(比如 RF*)。现有的 proc 意味着我的声明是这样的。

proc means data = input_table nway noprint missing;
   var age x y z RF: ST: ;
   class a b c;
   output out = output_table (drop = _type_ _freq_) max=;
run;

其中列 RF:指以 RF 开头的所有列,ST 也是如此。我想知道PROC SQL中是否有类似的东西可以使用?

谢谢!

【问题讨论】:

  • 您可能必须使用动态 SQL。这里有一篇文章:support.sas.com/resources/papers/proceedings12/070-2012.pdf
  • 这条语句是宏的一部分,我多次运行它,所以动态SQL可能会使它变得更加复杂。除此以外还有什么解决办法吗?
  • 我知道在 PROC SQL 中使其工作的唯一其他方法是规范化(“unpivot”)您的数据,以便列现在成行。根据您的数据集格式,这可能不可行。
  • 如果你有多个变量,为什么要切换,这是 SAS 最擅长的?

标签: sas


【解决方案1】:

如果您必须使用 SQL,动态 SQL 确实是解决这个问题的方法。好消息是你可以在一个 proc sql 调用中只使用一个宏变量来完成这一切,例如:

proc sql noprint;
  select catx(' ','max(',name,') as',name) into :MAX_LIST separated by ','
  from dictionary.columns
  where libname = 'SASHELP' 
  and memname = 'CLASS'
  and type = 'num'
  /*eq: is not available in proc sql in my version of SAS, but we can use substr to match partial variable names*/ 
  and upcase(substr(name,1,1)) in ('A','W') /*Match all numeric vars that have names starting with A or W*/
  ;
  create table want as select SEX, &MAX_LIST
    from sashelp.class
    group by SEX;
quit;

【讨论】:

  • 谢谢!这有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多