data s;
input start end;
cards;
10 20
15 40
25 60
26 70
55 85
40 75
35 45
15 60
25 72
85 45
;
run;
USing above dataset i want substract current observation of end - next observation of start
Iam tried below code but iam getting below note :
"Missing values were generated as a result of performing an operation on missing values.
Each place is given by: (Number of times) at (Line):(Column)."
data ssol ;
set s ;
lag1 = lag(end);
new = lag1-start ;
run;
You always get this when using lag, because there is not a lagged value for the first observation, so it is set to missing.
Based on your description it sounds you're rather after some "look ahead" and not some "look back" logic.
With both look ahead or look back you will always have at least one row (first or last) where the value is missing and though will have an operation with missing values. It's just a SAS Note so nothing wrong with this.
If in below code neither variable derived_1 nor derived_2 is what you want then please explain further your desired logic and ideally also provide a table WANT that show us the desired result if using your have table.
data have;
input start end;
cards;
10 20
15 40
25 60
26 70
55 85
40 75
35 45
15 60
25 72
85 45
;
data want;
merge have have(firstobs=2 keep=start rename=(start=_next_start));
derived_1 = end-_next_start ;
_lag_end=lag(end);
derived_2= _lag_end-start;
run;
proc print data=want;
run;
Try one of these (mind that results for the first observation differs):
data ssol1 ;
set s ;
lag1 = lag(end);
new = sum(lag1,-start);
run;
data ssol2 ;
set s ;
lag1 = lag(end);
if lag1>. then new = sum(lag1,-start);
run;
Bart
Conditionally (if/then) processing data can help avoiding such NOTEs in sas log when doing computation on data with missing values;
eg:
data ssol ;
set s ;
lag1 = lag(end);
if lag1 ne . then new = lag1-start;
proc print; run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.