如何从表中调用的是宏变量的变量?

时间:2019-04-19 17:33:26

标签: sas sas-macro

很抱歉,我的书名难以理解,但是我不知道该如何简短地描述它。

我正在尝试从表中调用一个宏变量(该表是一个宏变量)

我的宏看起来像这样:

%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”子句,但这似乎也不起作用。

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

1 个答案:

答案 0 :(得分:0)

您无需将摘要统计信息的选择存储在中间表中,以供以后在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>;

OR

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

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

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