Arrays 检查值的正确顺序
我有一个数据集,看起来类似于下面的一个。基本上,我有一个项目类型的三种不同尺寸的当前价格。如果大小定价正确,即小我看不出数组在这里有什么帮助。使用dif检查最后一个记录的价格,并验证它是否可以保留最后一个价格(如果您愿意的话)。确保数据集按类型、位置和大小正确排序,然后:Arrays 检查值的正确顺序,arrays,sas,Arrays,Sas,我有一个数据集,看起来类似于下面的一个。基本上,我有一个项目类型的三种不同尺寸的当前价格。如果大小定价正确,即小我看不出数组在这里有什么帮助。使用dif检查最后一个记录的价格,并验证它是否可以保留最后一个价格(如果您愿意的话)。确保数据集按类型、位置和大小正确排序,然后: 然后按类型位置将其合并回原始数据集,如果cur_price=1,则使用其他价格。不确定为什么要在此处使用数组…proc transpose和一些数据步骤逻辑 可以很容易地解决这个问题。数组非常有用我得承认我不完全是 对他们也很
然后按类型位置将其合并回原始数据集,如果cur_price=1,则使用其他价格。不确定为什么要在此处使用数组…proc transpose和一些数据步骤逻辑 可以很容易地解决这个问题。数组非常有用我得承认我不完全是 对他们也很满意,但在你有那么多地点的情况下, 我认为转置更好 下面的代码是否实现了您的目标
/*sorts to get ready for transpose*/
proc sort data=have;
by location;
run;
/*transpose current price*/
proc transpose data=have out=cur_tran prefix=cur_price;
by location;
id size;
var cur_price;
run;
/*transpose recommended price*/
proc transpose data=have out=rec_tran prefix=rec_price;
by location;
id size;
var rec_price;
run;
/*merge back together*/
data merged;
merge cur_tran rec_tran;
by location;
run;
/*creates flags and new field for final price*/
data want;
set merged;
if cur_priceS<cur_priceM<cur_priceL then
do;
FLAG='Y';
priceS=cur_priceS;
priceM=cur_priceM;
priceL=cur_priceL;
end;
else do;
FLAG='N';
priceS=rec_priceS;
priceM=rec_priceM;
priceL=rec_priceL;
end;
run;
或者,您可以在一个查询中完成它,这几乎是对您的需求的重新陈述
Proc sql;
create table want as
select *
/* Basically, I have current prices for three different sizes of an item type.
If the sizes are priced correctly (ie small<medium<large) */
, case
when max ( case when size eq 'S' then cur_price end)
lt max ( case when size eq 'M' then cur_price end)
and max ( case when size eq 'M' then cur_price end)
lt max ( case when size eq 'L' then cur_price end)
/* I want to flag them with a “Y” and continue to use the current price */
then 'Y'
/* If they are not priced correctly,
I want to flag them with a “N” and use the recommended price. */
else 'N'
end as Cur_Price_Sizes_Correct
, case
when calculate Cur_Price_Correct eq 'Y'
then cur_price
else rec_price
end as Price
From have
Group by Type
, Location
;
Quit;
下次小心点,谢谢你,乔。。我会记住这一点,下次我还提高了标题,请随意进一步提高它。标题应该描述你的问题,而不是你想如何解决它。请注意,如果我真的这样做,我会使用DoW循环来避免重蹈覆辙-但没有必要在这里介绍它。感谢这里的快速响应Joe-这正是我运行代码时需要的。我在SAS的经验不太丰富,所以我能更容易地从派尔那里得到答案
/*sorts to get ready for transpose*/
proc sort data=have;
by location;
run;
/*transpose current price*/
proc transpose data=have out=cur_tran prefix=cur_price;
by location;
id size;
var cur_price;
run;
/*transpose recommended price*/
proc transpose data=have out=rec_tran prefix=rec_price;
by location;
id size;
var rec_price;
run;
/*merge back together*/
data merged;
merge cur_tran rec_tran;
by location;
run;
/*creates flags and new field for final price*/
data want;
set merged;
if cur_priceS<cur_priceM<cur_priceL then
do;
FLAG='Y';
priceS=cur_priceS;
priceM=cur_priceM;
priceL=cur_priceL;
end;
else do;
FLAG='N';
priceS=rec_priceS;
priceM=rec_priceM;
priceL=rec_priceL;
end;
run;
Proc sql;
create table want as
select *
/* Basically, I have current prices for three different sizes of an item type.
If the sizes are priced correctly (ie small<medium<large) */
, case
when max ( case when size eq 'S' then cur_price end)
lt max ( case when size eq 'M' then cur_price end)
and max ( case when size eq 'M' then cur_price end)
lt max ( case when size eq 'L' then cur_price end)
/* I want to flag them with a “Y” and continue to use the current price */
then 'Y'
/* If they are not priced correctly,
I want to flag them with a “N” and use the recommended price. */
else 'N'
end as Cur_Price_Sizes_Correct
, case
when calculate Cur_Price_Correct eq 'Y'
then cur_price
else rec_price
end as Price
From have
Group by Type
, Location
;
Quit;