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

I'm looking to compare rows in the following way (assume dataset is sorted by FullCode, ChangeDt). Note: FullCode is simply ID concatenated with Type.

 

ID | Type | Source | Name | ChangeDt | StartDt | FullCode 

01 | 03 | Y | T | 08Aug1998 | 09Jul1998 | 0103

02 | 03 | Y | T | 08Aug1998 | 09Jul1998 | 0203

03 | 07 | O | T | 01May1994 | 13Jun1996 | 0307

03 | 07 | O | T | 20Jan1998 | 13Jun1996 | 0307

03 | 07 | N | T | 16Apr1999 | 13Jun1996 | 0307

 

So I want to create an iterator ID to count as we go down the rows per FullCode, when the ChangeDt is different, start at 1 and count up until you hit the next FullCode. Like below:

 

ID | Type | Source | Name | ChangeDt | StartDt | FullCode | Count

01 | 03 | Y | T | 08Aug1998 | 09Jul1998 | 0103 | 1

02 | 03 | Y | T | 08Aug1998 | 09Jul1998 | 0203 | 1

03 | 07 | O | T | 01May1994 | 13Jun1996 | 0307 | 1

03 | 07 | O | T | 20Jan1998 | 13Jun1996 | 0307 | 2

03 | 07 | N | T | 16Apr1999 | 13Jun1996 | 0307 | 3

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data have;
input ID Type Source $ Name $ (ChangeDt StartDt)(:date9.) FullCode;
format ChangeDt StartDt date9.;
infile datalines dlm='|';
datalines;
01|03|Y|T|08Aug1998|09Jul1998|0103
02|03|Y|T|08Aug1998|09Jul1998|0203
03|07|O|T|01May1994|13Jun1996|0307
03|07|O|T|20Jan1998|13Jun1996|0307
03|07|N|T|16Apr1999|13Jun1996|0307
;

data want;
   set have;
   by FullCode ChangeDt;
   if first.FullCode then Count=1;
   else if ChangeDt ne lag1(ChangeDt) then Count+1;
run;

 

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Do something like this

 

data have;
input ID Type Source $ Name $ (ChangeDt StartDt)(:date9.) FullCode;
format ChangeDt StartDt date9.;
infile datalines dlm='|';
datalines;
01|03|Y|T|08Aug1998|09Jul1998|0103
02|03|Y|T|08Aug1998|09Jul1998|0203
03|07|O|T|01May1994|13Jun1996|0307
03|07|O|T|20Jan1998|13Jun1996|0307
03|07|N|T|16Apr1999|13Jun1996|0307
;

data want;
   set have;
   by FullCode ChangeDt;
   if first.FullCode then Count=1;
   else if ChangeDt ne lag1(ChangeDt) then Count+1;
run;

 

novinosrin
Tourmaline | Level 20
data have;
input ID Type Source $ Name $ (ChangeDt StartDt)(:date9.) FullCode;
format ChangeDt StartDt date9.;
infile datalines dlm='|';
datalines;
01|03|Y|T|08Aug1998|09Jul1998|0103
02|03|Y|T|08Aug1998|09Jul1998|0203
03|07|O|T|01May1994|13Jun1996|0307
03|07|O|T|20Jan1998|13Jun1996|0307
03|07|N|T|16Apr1999|13Jun1996|0307
;

data want;
if 0 then set have;
do count=1 by 1 until(last.FullCode);
set have;
by  FullCode ChangeDt;
output;
end;
run;
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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 2 replies
  • 1214 views
  • 1 like
  • 3 in conversation