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

Current version: 9.04.01M5P091317

 

I need to find the non-consecutive integer ranges for a single column variable.

 

data have;
	infile datalines missover;
	input batchno;
datalines;
375
376
377
388
980
981
982
983
2008
2009
2010
;
run;

 

The output should provide beginning and ending values within each consecutive range. 

 

That is, the output is a list of the following values for the input dataset shown above:

 

375

388

980

983

2008

2010

 

Thank you in advance for assistance.

 

Jane

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

/*Going with my assumption  378 is what to be in place instead of 388*/

data have;
infile datalines missover; 
input batchno; 
datalines; 
375 
376 
377 
378
980
981
982 
983
2008 
2009
2010
;
run;

data temp;
set have;
k=dif(batchno);
if k ne 1 then grp+1;
drop k;
run;
data want;
set temp;
by grp;
if first.grp or last.grp;
drop grp;
run;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

HI @jawhitmire  

 

375

388  /*is this correct or should it be 378?*/

 

My apologies if i am asking a dumb question plz

novinosrin
Tourmaline | Level 20

/*Going with my assumption  378 is what to be in place instead of 388*/

data have;
infile datalines missover; 
input batchno; 
datalines; 
375 
376 
377 
378
980
981
982 
983
2008 
2009
2010
;
run;

data temp;
set have;
k=dif(batchno);
if k ne 1 then grp+1;
drop k;
run;
data want;
set temp;
by grp;
if first.grp or last.grp;
drop grp;
run;
mkeintz
PROC Star

You need

  1. A way to compare current batchno to the prior batchno.  If the prior batchno is not current-batchno minus 1, then the record-in-hand is a new START.
  2. A way to compare current batchno to the following batchno.  If they are not different by exactly 1, then the record-in hand is a new END.
  3. Only when you have an END value do you output the start/end pair.
data have;
	infile datalines missover;
	input batchno;
datalines;
375
376
377
388
980
981
982
983
2008
2009
2010
run;

data want (keep=start end);
  merge have have (firstobs=2 keep=batchno rename=(batchno=nxt_batchno));
  if batchno-1^=lag(batchno) then start=batchno;
  retain start;
  if batchno+1^=nxt_batchno;
  end=batchno;
run;

 

Comparing the current to the prior (lag(batchno)) is relatively intuitive.

 

Comparing to the next can't rely on the lag function, so you need a way to read in 2 records at a time: the current record and the following record.  Since they have the same variable, you have to rename one of them.  Then do the comparison ("if batchno+1 ^= nxt_batchno;").

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
jawhitmire
Quartz | Level 8

This worked perfectly and the output is clean.  It seems my questions are slowly becoming a bit more difficult.  I look forward to the day when I can contribute as much as I ask.

Thank you!

Jane

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 859 views
  • 1 like
  • 3 in conversation