DATA Step, Macro, Functions and more

Count number of a Session (Please help!)

Reply
Occasional Contributor
Posts: 11

Count number of a Session (Please help!)

Hi guys, 

 

I have a data set called T1 as follows:

dtUser_Catg_ID
01FEB15:18:34:424ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33008937
01FEB15:18:34:424ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33008937
01FEB15:18:34:424ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33008937
01FEB15:18:34:424ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33008937
01FEB15:18:34:424ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33008937
13MAR15:22:32:434ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33011524
13MAR15:22:32:434ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33011524
26FEB15:19:50:004ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33012808
26FEB15:19:50:004ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33012808
26FEB15:19:50:284ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33012808
26FEB15:19:50:284ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33012808
26FEB15:19:50:424ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33012808
26FEB15:19:50:424ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33012808
26FEB15:19:51:094ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33012808
26FEB15:19:51:094ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33012808
01FEB15:17:05:384ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33013130
01FEB15:17:05:384ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33013130
19FEB15:20:44:244ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:44:244ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:44:244ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:44:244ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:48:524ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:53:254ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:53:254ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:53:254ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
19FEB15:20:53:254ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33017029
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
02FEB15:19:55:514ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
03FEB15:18:54:504ecd9ffb-8661-4e0a-aa0b-529689a4a90b 33024975
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:17:16:164ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:18:38:194ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533
01FEB15:18:38:194ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533

 

 

I want to count the number of the session each person does. "dt" is the time that they visit the website and "User_Catg_ID" is their ID.  

once they visit the website their session is one, if they are inactive for more than 30 min and again active, the session should be increased by 1. 

for example :

dtUser_Catg_IDSession
18:305551
18:315551
18:355551
19:455552
19:475552
20:029851
20:039851
20:059851

 

I wrote the code but it does not work properly! could you please help me to fix this issue Smiley Sad

My code:

 

proc sort data=h.t1; by User_Catg_ID dt; run;
data h.t2;
set h.t1;
by User_Catg_ID;
retain session;
retain dt_r;
if first.User_Catg_ID=1 then session=1;
if first.User_Catg_ID=1 then dt_r = dt;
dt_diff = (dt-dt_r)/60;
if dt_diff > 30 then session+1=session;
else session=session;
dt_r = dt;
if last.User_Catg_ID=1 then user_last_pageview = 1;

drop dt_r dt_diff session;
run;

PROC Star
Posts: 2,370

Re: Count number of a Session (Please help!)

[ Edited ]
Posted in reply to Hassankalantari

Like this?

data h.t2;
  set h.t1;
  by User_Catg_ID;
  if first.User_Catg_ID=1 then session=1;
  else if dif(dt)> 60*30 then session+1;
run;
01FEB15:17:16:16 4ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533 1
01FEB15:17:16:16 4ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533 1
01FEB15:18:38:19 4ecd9ffb-8661-4e0a-aa0b-529689a4a90b 34955533 2

 

 

Ask a Question
Discussion stats
  • 1 reply
  • 46 views
  • 0 likes
  • 2 in conversation