<?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: How can I create a column that returns the number of months that have passed since a given event in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824644#M20467</link>
    <description>I have the code you want but every time I post it, it gets marked as SPAM for some reason!</description>
    <pubDate>Thu, 21 Jul 2022 13:34:38 GMT</pubDate>
    <dc:creator>stew90210</dc:creator>
    <dc:date>2022-07-21T13:34:38Z</dc:date>
    <item>
      <title>How can I create a column that returns the number of months that have passed since a given event?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824582#M20460</link>
      <description>&lt;P&gt;I'm creating a table to show the pattern of my users.&lt;/P&gt;&lt;P&gt;For each month since they joined, I have an indicator (Flag) that tells me if on that particular month the user watched tv or not. If Flag = 1 it means they used it, and if Flag = 0 it means they didn't used it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to create a new column that calculates for each month (each entry of the table) how many months have passed since the last time they watched tv.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you think you can help me write the code that creates this column? &lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Months.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73561i409D3950F3BCBAC0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Months.PNG" alt="Months.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 10:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824582#M20460</guid>
      <dc:creator>InêsMaximiano</dc:creator>
      <dc:date>2022-07-21T10:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824583#M20461</link>
      <description>&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    by userid;
    if first.user_id then months_since_last_event=0;
    if flag=1 then months_since_last_event=0;
    else if flag=0 then months_since_last_event+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Since we can't write code to read in data that is part of a screen capture, I can't test this code. You need to provide data in a usable form. From now on, please provide code as a SAS data step which you can type in yourself, or by following these &lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions&lt;/A&gt;, and not in any other format.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 11:17:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824583#M20461</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-21T11:17:58Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824592#M20462</link>
      <description>&lt;P&gt;hey buddy,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;not the tidiest code but I think this does what you want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data input;
input USERID $1 Year Month Flag 4.;
datalines;
A 2021 12 0
A 2022 1 1
A 2022 2 1
A 2022 3 0
A 2022 4 0
A 2022 5 0
B 2021 8 0
B 2022 9 1
B 2022 10 1
B 2021 11 0
B 2021 12 0
B 2022 1 0
;
run;


data step1;
retain flag date_cat months_since_last_event;
set input;
by userID ;
prev_flag =lag(flag);

if first.userID then do;
months_since_last_event =  .;
end;

else if flag =0 and prev_flag =1 then do;
months_since_last_event=1;
end;

else if flag =1 then do;
months_since_last_event=0;
end;


else do;
months_since_last_event+1;
end;
drop prev_flag;
run;

&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 11:10:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824592#M20462</guid>
      <dc:creator>stew90210</dc:creator>
      <dc:date>2022-07-21T11:10:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824597#M20463</link>
      <description>Thanks for your fast reply! I tried your code and it didnt work because I need it to restart everytime Flag = 1.&lt;BR /&gt;&lt;BR /&gt;Sorry, I didn't provide data in a usable form, I just wrote an example because I still don't have the exact data, but it would be something like this:&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;input UserID Year Month Flag;&lt;BR /&gt;datalines;&lt;BR /&gt;1 2021 10 0&lt;BR /&gt;1 2021 11 0&lt;BR /&gt;1 2021 12 1&lt;BR /&gt;1 2022 1 1&lt;BR /&gt;1 2022 2 0&lt;BR /&gt;1 2022 3 1&lt;BR /&gt;1 2022 4 0&lt;BR /&gt;2 2021 1 0&lt;BR /&gt;2 2021 2 0&lt;BR /&gt;2 2021 3 1&lt;BR /&gt;2 2021 4 1&lt;BR /&gt;2 2022 5 1&lt;BR /&gt;2 2022 6 0&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;I was also told I should use the function mdy to join the month and year of each line creating and auxiliar column and then use this to calculate the difference of months between each event (flag =1).&lt;BR /&gt;&lt;BR /&gt;I think the output should look like this:&lt;BR /&gt;&lt;BR /&gt;UserID Year Month Flag Months_since_event&lt;BR /&gt;1 2021 10 0 .&lt;BR /&gt;1 2021 11 0 .&lt;BR /&gt;1 2021 12 1 0&lt;BR /&gt;1 2022 1 1 1&lt;BR /&gt;1 2022 2 0 1&lt;BR /&gt;1 2022 3 1 2&lt;BR /&gt;1 2022 4 0 1&lt;BR /&gt;2 2021 1 0 .&lt;BR /&gt;2 2021 2 0 .&lt;BR /&gt;2 2021 3 1 0&lt;BR /&gt;2 2021 4 1 1&lt;BR /&gt;2 2022 5 1 1&lt;BR /&gt;2 2022 6 0 1&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 21 Jul 2022 11:27:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824597#M20463</guid>
      <dc:creator>InêsMaximiano</dc:creator>
      <dc:date>2022-07-21T11:27:21Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824599#M20464</link>
      <description>&lt;P&gt;Why does this line not have a 2 in the last column? Please explain.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;2 2022 5 1 1&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jul 2022 11:34:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824599#M20464</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-07-21T11:34:35Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824604#M20465</link>
      <description>2 2021 3 1 0&lt;BR /&gt;2 2021 4 1 1&lt;BR /&gt;2 2022 5 1 1&lt;BR /&gt;&lt;BR /&gt;---&amp;gt; In the first line its 0 because its the first time for that user that Flag =1&lt;BR /&gt;----&amp;gt; in the second line its 1 because its been 1 month since flag =1&lt;BR /&gt;----&amp;gt; in the third line (the one you questioned me about) its 1 again because its been 1 month since flag =1, it would be only 2 if the flag in the month before was 0 like this:&lt;BR /&gt;&lt;BR /&gt;2 2021 3 1 0&lt;BR /&gt;2 2021 4 1 1&lt;BR /&gt;2 2022 5 0 1&lt;BR /&gt;2 2022 6 0 2</description>
      <pubDate>Thu, 21 Jul 2022 11:48:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824604#M20465</guid>
      <dc:creator>InêsMaximiano</dc:creator>
      <dc:date>2022-07-21T11:48:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824644#M20467</link>
      <description>I have the code you want but every time I post it, it gets marked as SPAM for some reason!</description>
      <pubDate>Thu, 21 Jul 2022 13:34:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824644#M20467</guid>
      <dc:creator>stew90210</dc:creator>
      <dc:date>2022-07-21T13:34:38Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824662#M20471</link>
      <description>&lt;PRE&gt;data input;
input USERID $1 Year Month Flag 4.;
datalines;
A 2021 12 0
A 2022 1 1
A 2022 2 1
A 2022 3 0
A 2022 4 0
A 2022 5 0
B 2021 8 0
B 2022 9 1
B 2022 10 1
B 2021 11 0
B 2021 12 0
B 2022 1 0
;
run;


data step1;
retain flag date_cat months_since_last_event;
set input;
by userID ;
prev_flag =lag(flag);

if first.userID then do;
months_since_last_event =  .;
end;

else if flag =0 and prev_flag =1 then do;
months_since_last_event=1;
end;

else if flag =1 then do;
months_since_last_event=0;
end;


else do;
months_since_last_event+1;
end;
drop prev_flag;
run;

&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=""&gt;data input;
input USERID $1 Year Month Flag 4.;
datalines;
A 2021 12 0
A 2022 1 1
A 2022 2 1
A 2022 3 0
A 2022 4 0
A 2022 5 0
B 2021 8 0
B 2022 9 1
B 2022 10 1
B 2021 11 0
B 2021 12 0
B 2022 1 0
;
run;

data step1;
retain flag date_cat months_since_last_event;
set input;
by userID ;
prev_flag =lag(flag);
if first.userID then do;
months_since_last_event =  .;
end;
else if flag =0 and prev_flag =1 then do;
months_since_last_event=1;
end;
else if flag =1 then do;
months_since_last_event=0;
end;
else do;
months_since_last_event+1;
end;
drop prev_flag;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 21 Jul 2022 14:11:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824662#M20471</guid>
      <dc:creator>stew90210</dc:creator>
      <dc:date>2022-07-21T14:11:12Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824665#M20472</link>
      <description>Can you post an image of the code?</description>
      <pubDate>Thu, 21 Jul 2022 14:19:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824665#M20472</guid>
      <dc:creator>InêsMaximiano</dc:creator>
      <dc:date>2022-07-21T14:19:37Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824667#M20473</link>
      <description>&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 449px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73572i0625986E0C466756/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 14:21:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824667#M20473</guid>
      <dc:creator>stew90210</dc:creator>
      <dc:date>2022-07-21T14:21:11Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824679#M20474</link>
      <description>&lt;P&gt;Thanks for your time! However when I wrote the code you gave me I obtained the following output:&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Months.PNG" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73579iC9E6F4FC22D8D6E1/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Months.PNG" alt="Months.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 14:41:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824679#M20474</guid>
      <dc:creator>InêsMaximiano</dc:creator>
      <dc:date>2022-07-21T14:41:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824692#M20475</link>
      <description>sorry but that doesnt match what i get as an output and also your "coloumn I want" dosent make logical sense to me? in jan, feb and march of 22 user ID 1's results dont make any sense</description>
      <pubDate>Thu, 21 Jul 2022 15:10:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824692#M20475</guid>
      <dc:creator>stew90210</dc:creator>
      <dc:date>2022-07-21T15:10:19Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824699#M20476</link>
      <description>&lt;P&gt;Do you think its easier to understand like this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="InsMaximiano_0-1658416914097.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/73581iC45742CB14BD82E3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="InsMaximiano_0-1658416914097.png" alt="InsMaximiano_0-1658416914097.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jul 2022 15:22:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824699#M20476</guid>
      <dc:creator>InêsMaximiano</dc:creator>
      <dc:date>2022-07-21T15:22:14Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824700#M20477</link>
      <description>with regards to user ID 1; why is march 22 a 2 instead of a 0?  if dec 21 is a 0 then shouldn't it follow that it is the same? if you are trying to show how long its been since the last time they watched TV then surely whenever they are actually watching TV it should be a 0 to say its been no months since the last instance?</description>
      <pubDate>Thu, 21 Jul 2022 15:23:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824700#M20477</guid>
      <dc:creator>stew90210</dc:creator>
      <dc:date>2022-07-21T15:23:03Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824701#M20478</link>
      <description>Yeah I understand what you are saying and I had the same doubt... I was not sure if it was best to put zero on the month they are watching tv or just showing how many months it has been... Because from watching the table we know they watched tv on that month, but we don't see how months have passed since the last time they did it?!</description>
      <pubDate>Thu, 21 Jul 2022 15:29:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824701#M20478</guid>
      <dc:creator>InêsMaximiano</dc:creator>
      <dc:date>2022-07-21T15:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: How can I create a column that returns the number of months that have passed since a given event</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824721#M20480</link>
      <description>Ok well hopefully you have enough to get your started in my code! Come back if you get stuck</description>
      <pubDate>Thu, 21 Jul 2022 16:30:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-can-I-create-a-column-that-returns-the-number-of-months-that/m-p/824721#M20480</guid>
      <dc:creator>stew90210</dc:creator>
      <dc:date>2022-07-21T16:30:12Z</dc:date>
    </item>
  </channel>
</rss>

