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

Hi SAS Users,

 

Below is the data I have:

IDStartdateEnddateName
126-Sep-1326-Sep-13Erloti
123-Oct-1323-Oct-13Erloti
122-Nov-1322-Nov-13Erloti
116-Dec-1316-Dec-13Erloti
16-Jan-156-Jan-15Erloti
16-Jan-156-Jan-15Erloti
111-Feb-1511-Feb-15Erloti
111-Feb-1511-Feb-15Erloti
127-Aug-1327-Aug-13Erloti
120-Sep-1320-Sep-13Erloti
118-Oct-1318-Oct-13Erloti
120-Nov-1320-Nov-13Erloti
118-Dec-1318-Dec-13Erloti
110-Feb-1510-Feb-15Erloti
110-Feb-1510-Feb-15Erloti
31-Apr-141-Apr-14Crizot
325-Apr-1425-Apr-14Crizot
328-May-1428-May-14Crizot
323-Jun-1423-Jun-14Crizot
315-Jul-1415-Jul-14Ceriti
311-Aug-1411-Aug-14Ceriti
33-Sep-143-Sep-14Ceriti

I want to create a flag variable with the below condition

if Startdate - Lag(startdate ) > 60 or Name ne Lag(Name) then Flag = 1;

I want to create a count variable to have the total count on the Name changes per ID.

The final output expectation is below

IDStartdateEnddateNameFlagCount
126-Sep-1326-Sep-13Erloti0 
123-Oct-1323-Oct-13Erloti0 
122-Nov-1322-Nov-13Erloti0 
116-Dec-1316-Dec-13Erloti0 
16-Jan-156-Jan-15Erloti1 
16-Jan-156-Jan-15Erloti1 
111-Feb-1511-Feb-15Erloti1 
111-Feb-1511-Feb-15Erloti1 
127-Aug-1327-Aug-13Erloti1 
120-Sep-1320-Sep-13Erloti1 
118-Oct-1318-Oct-13Erloti1 
120-Nov-1320-Nov-13Erloti1 
118-Dec-1318-Dec-13Erloti1 
110-Feb-1510-Feb-15Erloti1 
110-Feb-1510-Feb-15Erloti11
31-Apr-141-Apr-14Crizot0 
325-Apr-1425-Apr-14Crizot0 
328-May-1428-May-14Crizot0 
323-Jun-1423-Jun-14Crizot0 
315-Jul-1415-Jul-14Ceriti1 
311-Aug-1411-Aug-14Ceriti1 
33-Sep-143-Sep-14Ceriti12

Thank you your time and expertise.

 

Have a great day.

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input ID	(Startdate Enddate)(:date11.)	Name $;
format Startdate Enddate date11.;
datalines;
1	26-Sep-13	26-Sep-13	Erloti
1	23-Oct-13	23-Oct-13	Erloti
1	22-Nov-13	22-Nov-13	Erloti
1	16-Dec-13	16-Dec-13	Erloti
1	6-Jan-15	6-Jan-15	Erloti
1	6-Jan-15	6-Jan-15	Erloti
1	11-Feb-15	11-Feb-15	Erloti
1	11-Feb-15	11-Feb-15	Erloti
1	27-Aug-13	27-Aug-13	Erloti
1	20-Sep-13	20-Sep-13	Erloti
1	18-Oct-13	18-Oct-13	Erloti
1	20-Nov-13	20-Nov-13	Erloti
1	18-Dec-13	18-Dec-13	Erloti
1	10-Feb-15	10-Feb-15	Erloti
1	10-Feb-15	10-Feb-15	Erloti
3	1-Apr-14	1-Apr-14	Crizot
3	25-Apr-14	25-Apr-14	Crizot
3	28-May-14	28-May-14	Crizot
3	23-Jun-14	23-Jun-14	Crizot
3	15-Jul-14	15-Jul-14	Ceriti
3	11-Aug-14	11-Aug-14	Ceriti
3	3-Sep-14	3-Sep-14	Ceriti
;

data want(drop=_:);
   set have;
   by ID;
   _lagstartdate=lag1(Startdate);
   _lagname=lag1(name);
   if first.ID then do;
      _lagstartdate=.;_lagname=.;
      _count=1;flag=.;
   end;
   else do;
      if (name ne _lagname) then _count+1;
      if (Startdate-_lagstartdate>60) | (name ne _lagname) then flag=1;
   end;

   if last.ID then count=_count;
   
   format _lagstartdate date11.;
   retain flag;
run;

View solution in original post

3 REPLIES 3
PeterClemmensen
Tourmaline | Level 20

Like this?

 

data have;
input ID	(Startdate Enddate)(:date11.)	Name $;
format Startdate Enddate date11.;
datalines;
1	26-Sep-13	26-Sep-13	Erloti
1	23-Oct-13	23-Oct-13	Erloti
1	22-Nov-13	22-Nov-13	Erloti
1	16-Dec-13	16-Dec-13	Erloti
1	6-Jan-15	6-Jan-15	Erloti
1	6-Jan-15	6-Jan-15	Erloti
1	11-Feb-15	11-Feb-15	Erloti
1	11-Feb-15	11-Feb-15	Erloti
1	27-Aug-13	27-Aug-13	Erloti
1	20-Sep-13	20-Sep-13	Erloti
1	18-Oct-13	18-Oct-13	Erloti
1	20-Nov-13	20-Nov-13	Erloti
1	18-Dec-13	18-Dec-13	Erloti
1	10-Feb-15	10-Feb-15	Erloti
1	10-Feb-15	10-Feb-15	Erloti
3	1-Apr-14	1-Apr-14	Crizot
3	25-Apr-14	25-Apr-14	Crizot
3	28-May-14	28-May-14	Crizot
3	23-Jun-14	23-Jun-14	Crizot
3	15-Jul-14	15-Jul-14	Ceriti
3	11-Aug-14	11-Aug-14	Ceriti
3	3-Sep-14	3-Sep-14	Ceriti
;

data want(drop=_:);
   set have;
   by ID;
   _lagstartdate=lag1(Startdate);
   _lagname=lag1(name);
   if first.ID then do;
      _lagstartdate=.;_lagname=.;
      _count=1;flag=.;
   end;
   else do;
      if (name ne _lagname) then _count+1;
      if (Startdate-_lagstartdate>60) | (name ne _lagname) then flag=1;
   end;

   if last.ID then count=_count;
   
   format _lagstartdate date11.;
   retain flag;
run;

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!

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
  • 3 replies
  • 1223 views
  • 1 like
  • 2 in conversation