参考下面的代码,在我转置数据集(输出qc2)后,我尝试创建一个百分比列(most_recent_wk_percent_change)但列的结果是12.5%,有两个新列 - & week3。和&周2。创建。预期结果是基于week2和week3列中的值进行计算。我知道问题可能是百分比计算中的两列的引用(==>(& week3。 - & week2。)/& week2。;),但我无法理解纠正。请告知:)
%let week1 = 7;
%let week2 = 8;
%let week3 = 9;
proc sql;
create table qc as
select t_week, prod_cat, sum(sales) as sales
from master_table
where t_week in (&week1.,&week2.,&week3.)
group by 1,2
order by 2;
quit;
proc transpose data= qc out=qc2;
format
by prod_cat ;
id t_week;
run;
data qc2;
set qc2;
format most_recent_wk_percent_change PERCENT7.1;
most_recent_wk_percent_change = ( &week3. - &week2.)/&week2.;
run;
qc:
t_week|prod_cat|sales
7|cat|100
8|cat|200
9|cat|300
7|dog|150
8|dog|400
9|dog|300
7|rat|200
8|rat|600
9|rat|300
qc2: (TRANSPOSED TABLE --> note the column name of 7,8,9. (which is expected)
prod_cat|7|8|9
cat|100|200|300
dog|150|400|300
rat|200|600|300
qc2: (i wanted to get the change in % )
prod_cat|7|8|9|most_recent_wk_percent_change|&week2.|&week3.
cat|100|200|300|12.5%|.|.| ==> 12.5% is wrong. should be 50% (300-200)/(200)
dog|150|400|300|12.5%|.|.| ==> 12.5% is wrong. should be -25%
rat|200|600|300|12.5%|.|.| ==> 12.5% is wrong. should be -50%
答案 0 :(得分:0)
尝试在上一个数据步骤中添加一个keep语句,这样只会保留输出中所需的列。
data qc2 (keep= most_recent_wk_percent_change prod_cat);
set qc2;
format most_recent_wk_percent_change PERCENT7.1;
most_recent_wk_percent_change = ( &week3. - &tweek2.)/&week2.;
run;
答案 1 :(得分:0)
我不知道你在做什么或为什么,但是如果你设置了VALIDVARNAME=any
并且变量的实际名称是7
,那么你尝试在SAS代码中使用它,如下所示:< / p>
ratio = 7/8 ;
然后SAS会假设您的意思是数值7.
您需要使用名称文字。
ratio = '7'n / '8'n ;
所以你想要
most_recent_wk_percent_change = ("&week3"n-"&week2"n)/"&week2"n;
如果变量的实际名称是_7
,那么您需要以这种方式编码。
most_recent_wk_percent_change = (_&week3.-_&week2.)/_&week2.;