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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- selecting a specific sequence of a variable

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-08-2017 10:52 AM

Hi,

I have the following data:

data have; input number sequence @@; datalines; 1 1 2 1 3 1 4 2 5 1 6 2 7 1 8 1 9 3 10 1 ; run;

What I would like to obtain are the observations for which the sequence =1 and the sequence of the number right after it is greater than 1:

number | sequence |

3 | 1 |

4 | 2 |

5 | 1 |

6 | 2 |

8 | 1 |

9 | 3 |

I was trying to solve this problem with a temporary array that serves as a rolling window:

```
data want;
set have;
array seq [2] _temporary_;
seq [*] = sequence;
if seq[2] > 1 and seq[1] = 1;
run;
```

But was getting an error for the part "seq[*] = sequence" that "Expecting an arihmetic expression"

Thank you!

Accepted Solutions

Solution

04-08-2017
08:41 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-08-2017 11:26 AM

One way to solve it is by using the lag function. e.g.:

data want (keep=out: rename=(out_sequence=sequence out_number=number)); set have; last_sequence=lag(sequence); last_number=lag(number); if last_sequence eq 1 and sequence gt 1 then do; out_sequence=last_sequence; out_number=last_number; output; out_sequence=sequence; out_number=number; output; end; run;

Art, CEO, AnalystFinder.com

All Replies

Solution

04-08-2017
08:41 PM

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-08-2017 11:26 AM

One way to solve it is by using the lag function. e.g.:

data want (keep=out: rename=(out_sequence=sequence out_number=number)); set have; last_sequence=lag(sequence); last_number=lag(number); if last_sequence eq 1 and sequence gt 1 then do; out_sequence=last_sequence; out_number=last_number; output; out_sequence=sequence; out_number=number; output; end; run;

Art, CEO, AnalystFinder.com

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-08-2017 12:07 PM

And here is another way to obtain the same result .. although I think using my first suggestion would be slightly faster:

data want (drop=_:); merge have have(firstobs=2 keep=sequence rename=(sequence=_sequence)); retain _next; if sequence eq 1 and _sequence gt 1 then do; output; _next=1; end; else if _next then do; output; _next=0; end; run;

Art, CEO, AnalystFinder.com

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content

04-08-2017 02:23 PM - edited 04-08-2017 02:46 PM

Hi,

data want(drop=flag);

do until (last.sequence);

set have end=last;

by sequence notsorted;

retain flag;

if last.sequence and sequence=1 and not last then do;

output;

flag=1;

end;

else if first.sequence and flag then do;

output;

flag=0;

end;

end;

run;

Regards,

Naveen Srinivasan