【问题标题】:How can I call a variable from a table that is a macro variable?如何从作为宏变量的表中调用变量?
【发布时间】:2019-04-19 17:33:25
【问题描述】:

如果我的标题读起来令人困惑,我深表歉意,但我不知道如何简要描述它。

我试图从一个宏变量的表中调用一个变量(该表是一个宏变量)

我的宏是这样的:

%macro genre_analysis(table1=,table2=,genre=,genre1=);
proc sql;
create table &table1 as
select id, original_title, genres, revenue
from genres_revenue
where genres_revenue.genres like &genre
and revenue is not null
group by id
having revenue ne 0
;
quit;

proc sql;
create table &table2 as
select avg(revenue) as Average format=dollar16.2, median(revenue) as Median format=dollar16.2, std(revenue) as std format=dollar16.2
from &table1;
quit;

在我进入宏的这一部分之前一切正常:

proc sql;
title "Revenue Stats by Genre";
    insert into genre_summary
    set Genre=&genre1,
    average=&table2.average,
    median=&table2.median,
    std=&table2.std;
%mend genre_analysis;

我正在尝试将一行插入到我在宏之外创建的表中。但是使用“&table2.average”和以“&table2”开头的其他两个不会调用我在宏中创建的表中的变量。

例如:

%genre_analysis(table1=horror_revenue,table2=horror_revenue_stats,genre='%Horror%',genre1='Horror')

返回:

NOTE: Table WORK.HORROR_REVENUE created, with 725 rows and 4 columns.

NOTE: PROCEDURE SQL used (Total process time):
      real time           0.04 seconds
      cpu time            0.03 seconds


NOTE: Table WORK.HORROR_REVENUE_STATS created, with 1 rows and 3 columns.

NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


ERROR: Character expression requires a character format.
ERROR: Character expression requires a character format.
ERROR: Character expression requires a character format.
ERROR: It is invalid to assign a character expression to a numeric value using the SET clause.
ERROR: It is invalid to assign a character expression to a numeric value using the SET clause.
ERROR: It is invalid to assign a character expression to a numeric value using the SET clause.
**ERROR: The following columns were not found in the contributing tables:
       horror_revenue_statsaverage, horror_revenue_statsmedian, horror_revenue_statsstd.**

我一直专注于我主演的错误,因为我相信这就是问题所在。

我尝试使用“from”子句,但这似乎也不起作用。

任何帮助或建议将不胜感激!

【问题讨论】:

  • 不知道为什么你在insert上方有一个title
  • 我也是,我会删除它。

标签: sas sas-macro


【解决方案1】:

您无需将选择的汇总统计信息存储在中间表中以供以后在INSERT 语句中使用。相反,您可以将所选内容直接插入到表格中。

例如:

* table to receive summary computations;
proc sql;
  create table stats
  (age num
  , average num
  , median  num
  , std     num
  , N num
  );

* insert summary computations directly;    
proc sql;
  insert into stats
  select 
     age
   , mean (height) 
   , median (height)
   , std (height)
   , count (height) 
   from sashelp.class
   group by age
  ;

* insert more summary computations directly;    

对于需要将 new-results 添加到 collecting-table 的一个表中的情况,您可以这样做

proc append base=<collecting-table> data=<new_results>;

insert into <collecting-table> select * from <new-results>;

最后,对于宏变量本身有一些新结果值的情况,您可以使用VALUE 子句插入这些值。您对将映射到字符列的任何宏变量解析进行字面引用。

insert into <collecting-table> values ("&genre", &genre.mean, ...);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-02
    • 2014-01-11
    • 2018-06-30
    • 2019-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多