如何从作为宏变量的 table 中调用变量?
How can I call a variable from a table that is a macro variable?
如果我的标题读起来让人困惑,我深表歉意,但我不知道如何简单地描述它。
我正在尝试从作为宏变量的 table 调用一个变量(table 是一个宏变量)
我的宏看起来像这样:
%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;
我正在尝试将一行插入我在宏之外创建的 table。但是使用“&table2.average”和其他两个以“&table2”开头的变量不会调用我在宏中创建的 table 中的变量。
例如:
%genre_analysis(table1=horror_revenue,table2=horror_revenue_stats,genre='%Horror%',genre1='Horror')
Returns:
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" 子句,但这似乎也不起作用。
如有任何帮助或建议,我们将不胜感激!
您不需要在中间 table 中存储选择的摘要统计信息以供以后在 INSERT
语句中使用。相反,您可以将选择直接插入 table.
例如:
* 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在一个table中的情况需要添加到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, ...);
如果我的标题读起来让人困惑,我深表歉意,但我不知道如何简单地描述它。
我正在尝试从作为宏变量的 table 调用一个变量(table 是一个宏变量)
我的宏看起来像这样:
%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;
我正在尝试将一行插入我在宏之外创建的 table。但是使用“&table2.average”和其他两个以“&table2”开头的变量不会调用我在宏中创建的 table 中的变量。
例如:
%genre_analysis(table1=horror_revenue,table2=horror_revenue_stats,genre='%Horror%',genre1='Horror')
Returns:
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" 子句,但这似乎也不起作用。
如有任何帮助或建议,我们将不胜感激!
您不需要在中间 table 中存储选择的摘要统计信息以供以后在 INSERT
语句中使用。相反,您可以将选择直接插入 table.
例如:
* 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在一个table中的情况需要添加到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, ...);