【问题标题】:SAS Creating a lag based on starting priceSAS 根据起始价格创建滞后
【发布时间】:2015-03-12 19:30:49
【问题描述】:

我的数据集包含 n 周内 x 多个位置的几件商品的价格。我希望创建一个变量,对于任何给定的位置/周,显示原始价格(列出的第一个)。我的数据集如下所示:

data have;
    input item $ location $ week price;
cards;
X NC 1 10
X NC 2 10
X NC 3 9.75
X SC 2 8
X SC 3 5
Y NC 1 100
Y NC 2 75
Y NC 3 50
Y NC 4 50
;
run;

我想要一个如下所示的数据集:

data want;
    input item $ location $ week price start_price;
cards;
X NC 1 10 10
X NC 2 10 10
X NC 3 9.75 10 
X SC 2 8 8
X SC 3 5 8
Y NC 1 100 100
Y NC 2 75 100
Y NC 3 50 100
Y NC 4 50 100
;
run;

我知道我可能会使用第一个。变量以某种方式执行此操作,但无法对其进行排序。帮忙?

我试过这个,但看起来我需要多个分组才能正确显示位置....我需要连接项目/位置还是有更优雅的方法来做到这一点?

data want;
    set have;
    by item;
    if first.item then start_price=price;
    start_price+0;
run;

【问题讨论】:

    标签: sas


    【解决方案1】:

    我会使用retain 来保留最后一行的值。结果将与 + 0 的 sum 语句相同,但我认为更有意义。

    如果我对问题的理解正确,您希望 first.location 设置 start_price。只需使用by item location; 即可。

    data want;
        set have;
        by item location;
        retain start_price;
        if first.location then start_price=price;
    run;
    

    【讨论】:

      【解决方案2】:

      只是为了演示如何获得组中的第一条记录,这里还有一个PROC SQL 解决方案

      data have;
          input item $ location $ week  price;
      cards;
      X NC 1 10
      X NC 2 10
      X NC 3 9.75
      X SC 2 8
      X SC 3 5
      Y NC 1 100
      Y NC 2 75
      Y NC 3 50
      Y NC 4 50
      ;
      run;
      

      首先使用单独的查询语句

      proc sql;
       create table START_PRICE as
       select Item, Location, Price as Start_Price
       from HAVE a
       where Week =
       (select min(week)
       from have b
       where a.item=b.item and a.location=b.location)
       order by a.item, a.location;
      
       Create table WANT as 
       Select a.item, a.location, a.week, a.price, b.start_price
       From HAVE a left join START_PRICE b
        on a.item=b.item and a.location=b.location
       order by a.item, a.location, a.week;
      Quit;
      

      然后作为一个查询

      Proc Sql ;
        Create table WANT2 as
          Select a.Item, a.Location, a.Week, a.Price, b.Start_Price
          from HAVE a
              Left Join
              (select Item, Location, Price as Start_Price
               from HAVE a1
               where Week =
                (select min(week) from have b1
                 where a1.item=b1.item and a1.location=b1.location)
                ) b
              on a.item=b.item and a.location=b.location
          order by a.item, a.location, a.week;
      Quit;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-18
        • 1970-01-01
        • 2022-07-20
        • 1970-01-01
        • 2017-01-05
        相关资源
        最近更新 更多