BookmarkSubscribeRSS Feed
112211
Obsidian | Level 7

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;

4 REPLIES 4
rudfaden
Pyrite | Level 9

You always get this when using lag, because there is not a lagged value for the first observation, so it is set to missing. 

Patrick
Opal | Level 21

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;

Patrick_0-1696572507557.png

 

yabwon
Onyx | Level 15

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

 

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



A_Kh
Lapis Lazuli | Level 10

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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 586 views
  • 0 likes
  • 5 in conversation