简体   繁体   中英

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..

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM