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
- /
- SAS Procedures
- /
- Subsetting previous observations if a criteria is met

Options

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

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

Posted 07-03-2018 01:27 PM
(1187 views)

I am dealing with real time data from an indoor air monitor. When the monitor runs out of alcohol, the machine reads 0. I am interested in creating a subset of my data that only includes the "0" observations and the preceding 30 observations.

I think I may be able to do this with some form of an IF/THEN statement before OUTPUT, but I am unsure. Maybe something like this?

```
DATA library.air_30;
SET library.air;
IF concentration=0 then OUTPUT *previous 30 observations* library.air_30;
RUN;
```

Can anyone offer some help with this? Thanks in advance.

3 REPLIES 3

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

Maybe reverse the order of your data and then it becomes a bit easier to look forward rather than backwards?

@taylormblack wrote:

I am dealing with real time data from an indoor air monitor. When the monitor runs out of alcohol, the machine reads 0. I am interested in creating a subset of my data that only includes the "0" observations and the preceding 30 observations.

I think I may be able to do this with some form of an IF/THEN statement before OUTPUT, but I am unsure. Maybe something like this?

`DATA library.air_30; SET library.air; IF concentration=0 then OUTPUT *previous 30 observations* library.air_30; RUN;`

Can anyone offer some help with this? Thanks in advance.

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

It should be relatively straightforward, such as:

data want;

set have;

by concentration notsorted;

if concentration=0 and first.concentration;

do recnum = max(1, _n_ - 30) to _n_;

set have point=recnum;

output;

end;

run;

However, what happens in real life when the alcohol runs out? (I know, the party's over, and everyone goes home.) Could you have several observations in a row that have concentration=0? Would you be retrieving some observations over and over again?

******************** EDITED in light of new information.

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

Hahaha, unfortunately the party ends and the machine keeps reading “0” until we refill the alcohol cartridge. So yes, I guess I would have a lot of duplicates. Is there a way to just subset the first “0” observation and the 30 preceding?

Thank you so much for your help with this. I am still getting the hang of SAS.

Thank you so much for your help with this. I am still getting the hang of SAS.

**SAS Innovate 2025** is scheduled for May 6-9 in Orlando, FL. Sign up to be **first to learn** about the agenda and registration!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Ready to level-up your skills? Choose your own adventure.