BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AZIQ1
Quartz | Level 8

Hi,

I want to flag a record where sequence is broken (0=no, 1=yes)


data have;
input account sequence
datalines;
1 1
1 2
1 3
2 2
2 3
3 1
3 2
run;


Want
Account Flag
1 1
2 0
3 1


1 ACCEPTED SOLUTION

Accepted Solutions
Haikuo
Onyx | Level 15

If your 'sequence' always starts from '1', here is a SQL option:

data have;
	input account sequence;
	datalines;
1 1
1 2
1 3
2 2
2 3
3 1
3 2
;

proc sql;
	create table want as
		select account, (count(distinct sequence)=max(sequence)) as flag
			from have
				group by account
	;
quit;

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

Simple

 

data have;
input account sequence;
datalines;
1 1
1 2
1 3
2 2
2 3
3 1
3 2
;

data want;
flag = 1;
do seq = 1 by 1 until (last.account);
    set have; by account;
    if seq ne sequence then flag = 0;
    end;
keep account flag;
run;

proc print; run;
PG
Astounding
PROC Star

You'll have to explain the problem a little better.  None of these sequences look broken to me.  So why do you want 1, 0, 1 as the results?

AZIQ1
Quartz | Level 8

Thank you , I want the sequence starting froom 1,2,3 if it does not have a 1 that means the sequence is broken (2,3) is not in order of 1,2 3.

Astounding
PROC Star

So does that mean that your "want" example data set is exactly the opposite of what you want?

Haikuo
Onyx | Level 15

If your 'sequence' always starts from '1', here is a SQL option:

data have;
	input account sequence;
	datalines;
1 1
1 2
1 3
2 2
2 3
3 1
3 2
;

proc sql;
	create table want as
		select account, (count(distinct sequence)=max(sequence)) as flag
			from have
				group by account
	;
quit;

SAS Innovate 2025: Save the Date

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

Save the date!

How to connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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