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

Hello Team,

 

 

I am stucked at one task.

 

 

BELOW IS THE SAMPLE DATA-SET

 

 

Number Status         Date  

101        active        06/08/2018

101        Inactive     07/08/2018

101        inactive     08/08/2018

102        inactive     05/08/2018

102        active        06/08/2018

102        Inactive     07/08/2018

102        inactive     08/08/2018

 

 

Suppose today is 08/08/2018 and Today's status is "Inactive" then I need to find out  how many days the status is "Inactive" for same number?

I tried in multiple ways like giving flag, using LAG function but i dint crack the code.

 

Please suggest any path so that I can go through that.

 

Thanks in advance.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
s_lassen
Meteorite | Level 14

Something like this may do:

data want;
  set have;
  by number;
  if first.number then
    days_inactive=0;
  if upcase(status)='ACTIVE' then
    days_inactive=0;
  else
    days_inactive+1;
run;

View solution in original post

4 REPLIES 4
LinusH
Tourmaline | Level 20
Try to use RETAIN to keep the last active date within a Number BY group.
Data never sleeps
Jagadishkatam
Amethyst | Level 16

please try the below code, if you need further modification, check the if then

 

data have;
input Number Status$         Date :ddmmyy10.;
status=propcase(status);
format date date9.;
cards; 
101        active        06/08/2018
101        Inactive     07/08/2018
101        inactive     08/08/2018
102        inactive     05/08/2018
102        active        06/08/2018
102        Inactive     07/08/2018
102        inactive     08/08/2018
;

proc sort data=have;
by number status;
run;

data want;
set have;
by number status;
retain new;
if first.status then new=date;
if status eq 'Inactive' then daysdiff=date-new;
format new date9.;
run;
Thanks,
Jag
s_manoj
Quartz | Level 8

Hi @india2016

data want (where = (name = "Inactive"));
set have;
by id name;
if first.name then start = date;
retain start;
if last.name  then do;
	end = date;
	duration = end - start + 1;
	output;
	end;
format start end date10.;
run;

Regards

Manoj

s_lassen
Meteorite | Level 14

Something like this may do:

data want;
  set have;
  by number;
  if first.number then
    days_inactive=0;
  if upcase(status)='ACTIVE' then
    days_inactive=0;
  else
    days_inactive+1;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1072 views
  • 4 likes
  • 5 in conversation