简体   繁体   English

如何从第一行减去第二行,从第三行减去第四行等等

[英]How to subtract second row from first, fourth row from third and so forth

I have SAS dataset as in the attached picture.. what I'm trying to accomplish is created new calculated field from Total column where I'm subtracting first row-second row, third row-fourth row and so on..我有 SAS 数据集,如所附图片中所示。

What i have tried so far is到目前为止我尝试过的是

DATA WANT2;
SET WANT;
BY APPT_TYPE;
IF FIRST.APPT_TYPE THEN SUPPLY-OPEN; ELSE 'ERROR';
RUN;

this throws an eror as statement is not valid..这会引发错误,因为语句无效..

not really sure how to go about this不太确定如何解决这个问题

My dataset我的数据集

Here you go.干得好。 The best I can do with the limited information you provided.我能用你提供的有限信息做到最好。 Next time please provide sample data and your expected output.下次请提供示例数据和您的预期输出。

data have;
input APPT_TYPE$ _NAME_$ Quantity;
datalines; 
ASON Supply 10
ASON Open 8
ASSN Supply 9
ASSN Open 7
S30 Supply 11
S30 Open 8
;

proc sort data = have;
    by APPT_TYPE descending _NAME_ ;
run;

data want;
    set have;
    by APPT_TYPE descending _NAME_;

    lag_N_Order = lag1(Quantity);
    N_Order = Quantity;
    Difference = lag_N_Order - N_Order;

    keep APPT_TYPE _NAME_ N_Order lag_N_Order Difference Type;
    if last.APPT_TYPE & last._NAME_ & Difference>0;
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用最早的更新第一行,使用第二个更新第二行,依此类推 - Update first row with oldest, the next with second oldest, and so forth 如何从第一行减去第二行并在mysql中生成输出列 - How to subtract second row from first row and generate a output column in mysql 在选择子查询中,如何从一行中选择一个值,然后从下一行中选择一个值,依此类推? - In a select sub query, how to select a value from one row, then select a value from the next row, and so forth? 将前两行相加作为第二行,然后将前三行相加作为第三行,依此类推 - Adding first two rows result as a second row then addition of first three rows result as a third row and so on SQL Server:从第一行减去最后一行 - SQL Server : subtract last row from first row 如何从第2行中减去第1行并将结果插入SQL Server中的第3行中? - How to subtract row 1 from row 2 and insert the result in row 3 in SQL Server? 如何只选择SQL Server中的第三或第四行 - How to select just the third or fourth row in SQL Server SQL选择第一,第二,第三,第四,最后 - SQL Select First, Second, Third, Fourth ,Last 如何从另一个MySQL减去一行 - How to subtract the one row from another MySQL 当第一个表中的行被删除时,如何从第二个表中删除数据 - how delete data from second table when row in first are deleting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM