<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Need to count number of the session by using retain, (please help I already spend 3 days on it : in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465914#M118852</link>
    <description>&lt;P&gt;Please try this code and refer to Revised_Session_Count column. Please confirm if it helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FILENAME F1 '/folders/myfolders/sasuser.v94/flat files/Session Calc.xlsx';&lt;/P&gt;&lt;P&gt;PROC IMPORT DATAFILE=F1&lt;BR /&gt;OUT=TEMP_T&lt;BR /&gt;DBMS=XLSX&lt;BR /&gt;REPLACE;&lt;/P&gt;&lt;P&gt;DATA TEMP;&lt;BR /&gt;SET TEMP_T;&lt;BR /&gt;FORMAT DT_NEW datetime.;&lt;BR /&gt;KEEP User_Catg_ID session_T DT_New;* D_dt date;&lt;BR /&gt;&lt;BR /&gt;DT_New=INT(INPUT(DT,datetime.));&lt;BR /&gt;*INFORMAT DT datetime16.; /*INFORMAT statement couldn't read DT as datetime value. Not sure why. SO used INPUT() with same informat and it worked just fine*/&lt;BR /&gt;&lt;BR /&gt;RENAME DT_NEW=DT;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*----Deleting TEMP_T dataset---*/&lt;BR /&gt;PROC DATASETS NOLIST LIB=work;&lt;BR /&gt;DELETE TEMP_T;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;PROC SORT DATA=TEMP;&lt;BR /&gt;BY User_Catg_ID DT;&lt;/P&gt;&lt;P&gt;DATA TEMP_1;&lt;BR /&gt;SET TEMP;&lt;BR /&gt;BY User_Catg_ID;&lt;BR /&gt;&lt;BR /&gt;Prev_val=LAG(DT);&lt;BR /&gt;&lt;BR /&gt;IF NOT FIRST.User_Catg_ID THEN TIME_DIFF=DT-Prev_val; /*NOTE: Using LAG(DT) directly in place of Prev_Val returns some weird results in few scenarios*/&lt;BR /&gt;ELSE TIME_DIFF=0;&lt;BR /&gt;&lt;BR /&gt;RETAIN Revised_Session_Count 1;&lt;BR /&gt;&lt;BR /&gt;IF FIRST.User_Catg_ID THEN Revised_Session_Count=1;&lt;BR /&gt;ELSE IF TIME_DIFF &amp;gt; (30*60) and NOT FIRST.User_Catg_ID THEN Revised_Session_Count+1;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;DROP Prev_Val;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC PRINT DATA=TEMP_1;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
    <pubDate>Wed, 30 May 2018 06:46:49 GMT</pubDate>
    <dc:creator>mahesh146</dc:creator>
    <dc:date>2018-05-30T06:46:49Z</dc:date>
    <item>
      <title>Need to count number of the session by using retain, (please help I already spend 3 days on it :( )</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465893#M118844</link>
      <description>&lt;P&gt;Hi guys,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset called T1&amp;nbsp;its attached&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to count the number of the session each person does. "dt" is the time that they visit the website and "&lt;SPAN&gt;User_Catg_ID"&amp;nbsp;&lt;/SPAN&gt;is their ID.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example :&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="4.PNG" style="width: 250px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/20842i9CB5A005B6EFD9E6/image-size/large?v=v2&amp;amp;px=999" role="button" title="4.PNG" alt="4.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;My code:I wrote the code but it does not work properly! could you please help me to fix this issue &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="123.PNG" style="width: 479px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/20841i7094ECBCD196DA1E/image-size/large?v=v2&amp;amp;px=999" role="button" title="123.PNG" alt="123.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 30 May 2018 04:02:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465893#M118844</guid>
      <dc:creator>Hassankalantari</dc:creator>
      <dc:date>2018-05-30T04:02:32Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of the session by using retain, (please help I already spend 3 days on it :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465900#M118847</link>
      <description>Can you please share the sample format in which you need the final dataset to look like?&lt;BR /&gt;</description>
      <pubDate>Wed, 30 May 2018 05:41:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465900#M118847</guid>
      <dc:creator>mahesh146</dc:creator>
      <dc:date>2018-05-30T05:41:46Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of the session by using retain, (please help I already spend 3 days on it :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465901#M118848</link>
      <description>&lt;P&gt;like this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input dt :time5. User_Catg_ID $;
format dt time5.;
datalines;
18:30 555
18:31 555
18:35 555
19:45 555
19:47 555
20:02 985
20:03 985
20:05 985
;

proc sort data=have presorted;
   by User_Catg_ID dt;
run;

data want;
   set have;
   retain session;
   by User_Catg_ID;
   if not first.User_Catg_ID and dif(dt) &amp;gt; 30*60 then session+1;
   if first.User_Catg_ID then session=1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 May 2018 05:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465901#M118848</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-05-30T05:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of the session by using retain, (please help I already spend 3 days on it :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465902#M118849</link>
      <description>&lt;P&gt;Try this:-&lt;/P&gt;&lt;PRE&gt; data h.t2;
    set h.t1;
    by User_Catg_ID;
    retain session;
    retain dt_r;
    if first.User_Catg_ID then session=1;
    else
        do;
	    dt_diff = (dt-dt_r)/60;
	    if dt_diff &amp;gt; 30 then session=session+1;
	end;
    dt_r=dt;
    if last.User_Catg_ID then
        do;
            user_last_pageview=session;
            drop dt_r dt_diff session;
            output;
        end;
run;&lt;/PRE&gt;</description>
      <pubDate>Wed, 30 May 2018 05:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465902#M118849</guid>
      <dc:creator>Nartlada</dc:creator>
      <dc:date>2018-05-30T05:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of the session by using retain, (please help I already spend 3 days on it :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465910#M118850</link>
      <description>&lt;P&gt;Hi Guys,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks alot for the reply,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I&amp;nbsp; tried below code and it works, I also will use your codes! Thanks again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data h.Imp_Rev_x2;&lt;BR /&gt;set h.Imp_Rev_x1;&lt;BR /&gt;retain session;&lt;BR /&gt;by User_Catg_ID;&lt;BR /&gt;retain dt_r;&lt;BR /&gt;if first.User_Catg_ID then session=1;&lt;BR /&gt;if first.User_Catg_ID then dt_r = dt;&lt;BR /&gt;dt_diff = (dt-dt_r)/60;&lt;BR /&gt;if dt_diff &amp;gt; 30 then session=session+1;&lt;BR /&gt;if dt_diff &amp;lt;= 30 then session=session;&lt;BR /&gt;dt_r = dt;&lt;BR /&gt;if last.User_Catg_ID=1 then user_last_pageview = 1;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 May 2018 06:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465910#M118850</guid>
      <dc:creator>Hassankalantari</dc:creator>
      <dc:date>2018-05-30T06:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of the session by using retain, (please help I already spend 3 days on it :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465914#M118852</link>
      <description>&lt;P&gt;Please try this code and refer to Revised_Session_Count column. Please confirm if it helps.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;FILENAME F1 '/folders/myfolders/sasuser.v94/flat files/Session Calc.xlsx';&lt;/P&gt;&lt;P&gt;PROC IMPORT DATAFILE=F1&lt;BR /&gt;OUT=TEMP_T&lt;BR /&gt;DBMS=XLSX&lt;BR /&gt;REPLACE;&lt;/P&gt;&lt;P&gt;DATA TEMP;&lt;BR /&gt;SET TEMP_T;&lt;BR /&gt;FORMAT DT_NEW datetime.;&lt;BR /&gt;KEEP User_Catg_ID session_T DT_New;* D_dt date;&lt;BR /&gt;&lt;BR /&gt;DT_New=INT(INPUT(DT,datetime.));&lt;BR /&gt;*INFORMAT DT datetime16.; /*INFORMAT statement couldn't read DT as datetime value. Not sure why. SO used INPUT() with same informat and it worked just fine*/&lt;BR /&gt;&lt;BR /&gt;RENAME DT_NEW=DT;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*----Deleting TEMP_T dataset---*/&lt;BR /&gt;PROC DATASETS NOLIST LIB=work;&lt;BR /&gt;DELETE TEMP_T;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;PROC SORT DATA=TEMP;&lt;BR /&gt;BY User_Catg_ID DT;&lt;/P&gt;&lt;P&gt;DATA TEMP_1;&lt;BR /&gt;SET TEMP;&lt;BR /&gt;BY User_Catg_ID;&lt;BR /&gt;&lt;BR /&gt;Prev_val=LAG(DT);&lt;BR /&gt;&lt;BR /&gt;IF NOT FIRST.User_Catg_ID THEN TIME_DIFF=DT-Prev_val; /*NOTE: Using LAG(DT) directly in place of Prev_Val returns some weird results in few scenarios*/&lt;BR /&gt;ELSE TIME_DIFF=0;&lt;BR /&gt;&lt;BR /&gt;RETAIN Revised_Session_Count 1;&lt;BR /&gt;&lt;BR /&gt;IF FIRST.User_Catg_ID THEN Revised_Session_Count=1;&lt;BR /&gt;ELSE IF TIME_DIFF &amp;gt; (30*60) and NOT FIRST.User_Catg_ID THEN Revised_Session_Count+1;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;DROP Prev_Val;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC PRINT DATA=TEMP_1;&lt;BR /&gt;RUN;&lt;/P&gt;</description>
      <pubDate>Wed, 30 May 2018 06:46:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465914#M118852</guid>
      <dc:creator>mahesh146</dc:creator>
      <dc:date>2018-05-30T06:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: Need to count number of the session by using retain, (please help I already spend 3 days on it :</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465933#M118859</link>
      <description>&lt;P&gt;Great!&lt;/P&gt;&lt;P&gt;I dont have the data now, but I will try and let you know the result!!&lt;/P&gt;&lt;P&gt;Thanks alot!&lt;/P&gt;</description>
      <pubDate>Wed, 30 May 2018 07:59:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Need-to-count-number-of-the-session-by-using-retain-please-help/m-p/465933#M118859</guid>
      <dc:creator>Hassankalantari</dc:creator>
      <dc:date>2018-05-30T07:59:30Z</dc:date>
    </item>
  </channel>
</rss>

