我在excel中有数据集,以变量商店为空格和&在数据中。我想根据商店和家人做一些报告和图表。但是客户希望保留商店名称。例如:taba& wv,sup top。如何通过商店使用这种名称添加MACRO来生成表格,以及如何通过商店使用宏生成报告,如PROC REPORT,PROC TABULATE和CHARTS。请在下面找到示例数据
Shop Year Month Family Value1 Value2 Value3
raoas 2006 january TA12 5 6 0
taba&wv 2008 january TS01 0 1 1
sup top 2008 april TZ05 0 0 1
taba&wv 2006 December TA12 5 6 0
raoas 2008 january TA15 0 2 0
sup top 2008 april TQ05 0 1 1
我想要创建的表格应该是
taba&wv
Year Family sum_value1 Sumvalue2 Sum_value3
sup top
Year Family sum_value1 Sumvalue2 Sum_value3
raoas
Year Family sum_value1 Sumvalue2 Sum_value3
答案 0 :(得分:2)
如果您只需要针对不同商店的单独报告(因此不需要使用商店名称命名数据集或变量),那么您可以正确地屏蔽正确的商店标题。例如,这里为每个商店创建PROC TABULATE:
data have;
infile datalines dsd dlm=',';
input Shop $ Year Month $ Family $ Value1 Value2 Value3 ;
datalines;
raoas,2006,january,TA12,5,6,0
taba&wv,2008,january,TS01,0,1,1
sup top,2008,april,TZ05,0,0,1
taba&wv,2006,December,TA12,5,6,0
raoas,2008,january,TA15,0,2,0
sup top,2008,april,TQ05,0,1,1
;run;
proc sql noprint;
select distinct shop into :shops separated by '|'
from have;
quit;
%macro reports;
%do i=1 %to %sysfunc(countw(%superq(shops),|));
title "PROC TABULATE for %qscan(%superq(shops),&i,|)";
proc tabulate data=have(where=(shop="%qscan(%superq(shops),&i,|)"));
class year month family;
var value:;
table year*month*family
,
(value1 value2 value3)*sum;
run;
%end;
%mend reports;
%reports
%SUPERQ()
不允许宏编译器在扫描名称时尝试将taba&wv
解析为宏变量,并%QSCAN()
以及掩码&
和空白。
注意:Macrovariable作为%SUPERQ()
中的参数 - 函数必须没有&
。
答案 1 :(得分:1)
一个名称中的&
会产生问题。 SAS名称中不允许使用它。如果不使用VALIDVARNAME=ANY
操作,空间也会成为问题。我会在开头将两者转换为_
。
%macro create_tables(input,output);
data temp;
set &input;
Shop = translate(shop,'__','& ');
run;
%local n i;
proc sql noprint;
select count(distinct shop)
into :n
from temp;
%do i=1 %to &n;
%local v&i;
%end;
select distinct shop
into :v1 - :v%left(&n)
from temp;
%do i=1 %to &n
create table &&v&i as
select shop,
Family,
sum(Value1) as sum_value1,
sum(Value2) as sum_value2,
sum(Value3) as sum_value3
from temp
where shop = "&&v&i"
group by shop, family;
%end;
%quit;
%mend;
这使用PROC SQL将不同的商店变成一系列宏变量& v1 - & vN。然后,它使用SQL查询大表,并计算值的总和。