Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Home
- /
- Programming
- /
- Programming
- /
- Why am I getting a missing value message?

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

☑ This topic is **solved**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 11-06-2023 06:46 PM
(290 views)

27 data a;

28 input x 1. y 1.;

29 list;

30 datalines;

RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0

31 11

32 43

33 43

NOTE: The data set WORK.A has 3 observations and 2 variables.

NOTE: DATA statement used (Total process time):

real time 0.01 seconds

cpu time 0.03 seconds

34 ;

35 run;

36

37 data b;

38 set a;

39 by x y;

40 /*only do the comparison after the first record*/

41 if _n_=1 then do;

42 cnt=1;

43 end;

44 else do;

45 if x=lag(x) and y le (lag(y)+4) then cnt+0;

46 else cnt+1;

47 end;

48 run;

2 The SAS System 15:20 Monday, November 6, 2023

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

1 at 45:31

NOTE: There were 3 observations read from the data set WORK.A.

NOTE: The data set WORK.B has 3 observations and 3 variables.

NOTE: DATA statement used (Total process time):

real time 0.00 seconds

cpu time 0.00 seconds

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Please note that the lag() function only queues a value if it executes. Using this function conditionally leads most of the time to undesired results.

You get the missing note because the first time below code executes the lag() function will return a missing.

I believe logic like below should return what you're after.

```
data b(drop=_:);
set a;
by x y;
_lag_y=lag(y);
/*only do the comparison after the first record*/
if _n_=1 then
do;
cnt=1;
end;
else
do;
if not first.x and y le (_lag_y+4) then cnt+0;
else cnt+1;
end;
run;
```

And stripping down your logic further below should still return the same result:

```
data b(drop=_:);
set a;
by x y;
_lag_y=lag(y);
if first.x or y le (_lag_y+4) then cnt+1;
run;
```

And even below would still work

```
data b(drop=_:);
set a;
by x y;
cnt + ( first.x or y > sum(lag(y),4) );
run;
```

1 REPLY 1

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Please note that the lag() function only queues a value if it executes. Using this function conditionally leads most of the time to undesired results.

You get the missing note because the first time below code executes the lag() function will return a missing.

I believe logic like below should return what you're after.

```
data b(drop=_:);
set a;
by x y;
_lag_y=lag(y);
/*only do the comparison after the first record*/
if _n_=1 then
do;
cnt=1;
end;
else
do;
if not first.x and y le (_lag_y+4) then cnt+0;
else cnt+1;
end;
run;
```

And stripping down your logic further below should still return the same result:

```
data b(drop=_:);
set a;
by x y;
_lag_y=lag(y);
if first.x or y le (_lag_y+4) then cnt+1;
run;
```

And even below would still work

```
data b(drop=_:);
set a;
by x y;
cnt + ( first.x or y > sum(lag(y),4) );
run;
```

**Don't miss out on SAS Innovate - Register now for the FREE Livestream!**

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

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.