BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hello SAS Users,

I have a question regarding creating a data set by selecting a sequence of observations in SAS. I have a data set similar to this:

X, Y, COUNT
20, 100, 0
20, 100, 0
20, 100, 1
20, 100, 0
20, 100, 1
20, 100, 1
20, 100, 1
20, 100, 0

21, 101, 1
21, 101, 1
21, 101, 1
21, 101, 0
21, 101, 1
21, 101, 1
21, 101, 1
21, 101, 0

22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 0

Now, I need to select and output observations that have a continuous sequence of at least three 1's. For example, where X=20, Y=100, observations 5-7 should be selected; X=21, Y=101, observations 1-3 and 5-7 should be selected; X=22, Y=102, observation 1-7 should be selected. Can anybody offer any suggestions? Thanks in advance.
3 REPLIES 3
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
Using PROC SUMMARYwith a CLASS / BY (sorted) list, and then merge back (a DATA step MERGE with a BY and using IN=I_summary_candidate_var on the summarized file) the candidate summary-level observations back into your detail data (where non-zero condition) to select candidate detail observations.

Scott Barry
SBBWorks, Inc.
SushilNayak
Obsidian | Level 7
Hey sas_grad,
Here is a different solution to the problem.

data dy;
infile datalines dlm=',' missover;
input x y cnt;
datalines;
20, 100, 0
20, 100, 0
20, 100, 1
20, 100, 0
20, 100, 1
20, 100, 1
20, 100, 1
20, 100, 0
21, 101, 1
21, 101, 1
21, 101, 1
21, 101, 0
21, 101, 1
21, 101, 1
21, 101, 1
21, 101, 0
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 1
22, 102, 0
;
run;
data dy;
set dy;
by notsorted x y cnt;
retain count 0;
if first.cnt then count=_n_;
run;

proc sql;
create table final as
select a.x, a.y, a.cnt,b.count
from dy a
,( select x, y, cnt, count , count(count) as xx
from dy
group by x,y,cnt,count
having count(count) >= 3
) b
where a.count=b.count;
quit;
proc print;run;

Thanks!!!
deleted_user
Not applicable
Thanks a bunch, Sushil and Scott. Sushil, the overall logic of your code was really helpful in solving my problem.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 3 replies
  • 907 views
  • 0 likes
  • 3 in conversation