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

Hi Experts,

Require your help on this. I have tried my best with LAG function but didn't work for all the scenarios.

I have the TimeIn and TimeOut fields and have to create the Flag below. Thanks a lot for the help.

 

RowNo.TimeInTimeOutFlag
114SEP2020:08:02:3114SEP2020:08:02:31N
214SEP2020:08:02:5314SEP2020:08:04:19N
314SEP2020:08:03:3814SEP2020:08:04:57N
414SEP2020:08:15:2514SEP2020:08:25:39N
514SEP2020:08:20:1314SEP2020:08:20:38Y
614SEP2020:08:49:0114SEP2020:09:50:59N
714SEP2020:08:53:4314SEP2020:09:18:27Y
814SEP2020:09:17:3314SEP2020:10:07:03N
914SEP2020:09:24:4414SEP2020:09:26:08Y
1014SEP2020:09:27:1514SEP2020:10:00:23Y
1114SEP2020:09:43:2314SEP2020:09:43:26Y
1215SEP2020:08:02:3115SEP2020:08:02:31N
1315SEP2020:08:02:5315SEP2020:08:04:19N
1415SEP2020:08:03:3815SEP2020:08:04:57N
1515SEP2020:08:15:2515SEP2020:08:25:39N
1615SEP2020:08:20:1315SEP2020:08:20:38Y
1715SEP2020:08:49:0115SEP2020:09:50:59N
1815SEP2020:08:53:4315SEP2020:09:18:27Y
1915SEP2020:09:17:3315SEP2020:10:07:03N
2015SEP2020:09:24:4415SEP2020:09:26:08Y
2115SEP2020:09:27:1515SEP2020:10:00:23Y
2215SEP2020:09:43:2315SEP2020:09:43:26Y
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

So essentially the logic is that a call connected earlier than any previous call connected, then FLAG='Y'

 

data have;
input Row	TimeIn :datetime18.	TimeOut	 :datetime18. Flag $;
cards;
1	14SEP2020:08:02:31	14SEP2020:08:02:31	N
2	14SEP2020:08:02:53	14SEP2020:08:04:19	N
3	14SEP2020:08:03:38	14SEP2020:08:04:57	N
4	14SEP2020:08:15:25	14SEP2020:08:25:39	N
5	14SEP2020:08:20:13	14SEP2020:08:20:38	Y
6	14SEP2020:08:49:01	14SEP2020:09:50:59	N
7	14SEP2020:08:53:43	14SEP2020:09:18:27	Y
8	14SEP2020:09:17:33	14SEP2020:10:07:03	N
9	14SEP2020:09:24:44	14SEP2020:09:26:08	Y
10	14SEP2020:09:27:15	14SEP2020:10:00:23	Y
11	14SEP2020:09:43:23	14SEP2020:09:43:26	Y
12	15SEP2020:08:02:31	15SEP2020:08:02:31	N
13	15SEP2020:08:02:53	15SEP2020:08:04:19	N
14	15SEP2020:08:03:38	15SEP2020:08:04:57	N
15	15SEP2020:08:15:25	15SEP2020:08:25:39	N
16	15SEP2020:08:20:13	15SEP2020:08:20:38	Y
17	15SEP2020:08:49:01	15SEP2020:09:50:59	N
18	15SEP2020:08:53:43	15SEP2020:09:18:27	Y
19	15SEP2020:09:17:33	15SEP2020:10:07:03	N
20	15SEP2020:09:24:44	15SEP2020:09:26:08	Y
21	15SEP2020:09:27:15	15SEP2020:10:00:23	Y
22	15SEP2020:09:43:23	15SEP2020:09:43:26	Y
;

data want;
    set have;
    retain maxtimeout;
    if _n_=1 then maxtimeout=timeout;
    if timeout>=maxtimeout then do;
        flag='N';
        maxtimeout=timeout;
    end;
    else flag='Y';
run;
--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

I have the TimeIn and TimeOut fields and have to create the Flag below.

 

Instead of us trying to figure out what is the criteria for creating the variable FLAG (and possibly getting it wrong), I am asking you to clearly state the logic that creates the proper values of FLAG.

--
Paige Miller
DataPanda99
Fluorite | Level 6

Apologies for not explaining it properly earlier, I will try my best to explain here..

Let's take row number 19 onwards...Call entered at 09:17:33 and call connected at 10:07:03
and the next customer (row no-20) call entered at 09:24:44 and call connected at 09:26:08 (basically call connected earlier than the previous customer who was in a queue) and flagged as Y
and the next customers (21&22) calls connected earlier than 19th customer though they called after 19th customer so flagged as Y.

PaigeMiller
Diamond | Level 26

So essentially the logic is that a call connected earlier than any previous call connected, then FLAG='Y'

 

data have;
input Row	TimeIn :datetime18.	TimeOut	 :datetime18. Flag $;
cards;
1	14SEP2020:08:02:31	14SEP2020:08:02:31	N
2	14SEP2020:08:02:53	14SEP2020:08:04:19	N
3	14SEP2020:08:03:38	14SEP2020:08:04:57	N
4	14SEP2020:08:15:25	14SEP2020:08:25:39	N
5	14SEP2020:08:20:13	14SEP2020:08:20:38	Y
6	14SEP2020:08:49:01	14SEP2020:09:50:59	N
7	14SEP2020:08:53:43	14SEP2020:09:18:27	Y
8	14SEP2020:09:17:33	14SEP2020:10:07:03	N
9	14SEP2020:09:24:44	14SEP2020:09:26:08	Y
10	14SEP2020:09:27:15	14SEP2020:10:00:23	Y
11	14SEP2020:09:43:23	14SEP2020:09:43:26	Y
12	15SEP2020:08:02:31	15SEP2020:08:02:31	N
13	15SEP2020:08:02:53	15SEP2020:08:04:19	N
14	15SEP2020:08:03:38	15SEP2020:08:04:57	N
15	15SEP2020:08:15:25	15SEP2020:08:25:39	N
16	15SEP2020:08:20:13	15SEP2020:08:20:38	Y
17	15SEP2020:08:49:01	15SEP2020:09:50:59	N
18	15SEP2020:08:53:43	15SEP2020:09:18:27	Y
19	15SEP2020:09:17:33	15SEP2020:10:07:03	N
20	15SEP2020:09:24:44	15SEP2020:09:26:08	Y
21	15SEP2020:09:27:15	15SEP2020:10:00:23	Y
22	15SEP2020:09:43:23	15SEP2020:09:43:26	Y
;

data want;
    set have;
    retain maxtimeout;
    if _n_=1 then maxtimeout=timeout;
    if timeout>=maxtimeout then do;
        flag='N';
        maxtimeout=timeout;
    end;
    else flag='Y';
run;
--
Paige Miller
DataPanda99
Fluorite | Level 6

Thank you but it's slightly not working. 

I have added few more observations and when I run this it should come as N but coming in as Y.

 

2315SEP2020:08:55:3915SEP2020:09:27:12N
2415SEP2020:09:46:2815SEP2020:09:46:51N
2515SEP2020:10:01:5515SEP2020:10:02:30N
2615SEP2020:10:02:3715SEP2020:10:03:00N
PaigeMiller
Diamond | Level 26

Since ROW 19 was answered at 15SEP2020:10:07:03, why are any of these N? Please explain the logic.

--
Paige Miller
DataPanda99
Fluorite | Level 6

Thanks a lot! Now I got it. 🙂

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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
  • 6 replies
  • 1192 views
  • 0 likes
  • 2 in conversation