<?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: Assign variable values to time intervals in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987466#M43838</link>
    <description>Sorry I edited. ddmm were inverted in Ref_date. I edited accordingly.</description>
    <pubDate>Thu, 07 May 2026 12:15:53 GMT</pubDate>
    <dc:creator>NewUsrStat</dc:creator>
    <dc:date>2026-05-07T12:15:53Z</dc:date>
    <item>
      <title>Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987460#M43832</link>
      <description>&lt;P&gt;Dear all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppose to have the following dataset:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. (Ref_date) (:mmddyy.) (Date_event) (:mmddyy.) Event :$20.;
  format Ref_date date9. Date_event date9.;
cards;
0001 09/01/2024  10/11/2024  Y
0001 09/01/2024  11/11/2024  Y
0001 09/01/2024       .      N
0001 09/01/2024  01/15/2025  Y
0002 09/11/2016  04/15/2016  Y
0003 30/06/2025  02/09/2025  Y
0003 30/06/2025  12/12/2025  Y
0004 12/03/2024       .      N
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Is there a way to get the following?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB1;
  input ID :$20. (Ref_date) (:mmddyy.) (Date_event) (:mmddyy.) Event :$20. Flag   :$20.;
  format Ref_date date9. Date_event date9.;
cards;
0001 09/01/2024  10/11/2024  Y  3M
0001 09/01/2024  11/11/2024  Y   .
0001 09/01/2024       .      N   .
0001 09/01/2024  01/15/2025  Y  6M
0002 09/11/2016  04/15/2016  Y  9M
0003 30/06/2025  02/09/2025  Y   .
0003 30/06/2025  12/12/2025  Y  6M 
0004 12/03/2024       .      N   .
;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The desired output is based on the following rules:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) if the Event = "Y" and date_event&amp;nbsp; &amp;gt; Ref_date then add a new column (variable) called "Flag" with values = 3M, 6M, 9M, 12M if the event occurs during the 3,6,9,12 months from the Ref_date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider that time intervals are intended as follows:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;from the Ref_date to 3M = 3M from the Ref_date&lt;/P&gt;
&lt;P&gt;between 3M and 6M =6M from the Ref_date&lt;/P&gt;
&lt;P&gt;between 6M and 9M =9M&amp;nbsp;from the Ref_date&lt;/P&gt;
&lt;P&gt;between 9M and 12M =12M&amp;nbsp;from the Ref_date&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can anyone help me please?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 13:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987460#M43832</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-07T13:21:16Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987461#M43833</link>
      <description>&lt;P&gt;Expected results, data and description don't match.&lt;/P&gt;
&lt;P&gt;In the first observation, from 9Jan2024 to 11Oct2024 is nine month, why do you expect 3M?&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 11:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987461#M43833</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2026-05-07T11:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987462#M43834</link>
      <description>&lt;P&gt;A couple of questions&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;In the first record of data set DB, it seems to me that 9 months have elapsed but you show this should have a value of 3M in variable FLAG. I don't understand this.&lt;/LI&gt;
&lt;LI&gt;You don't make clear if there is a different way to handle things when REF_DATE is &lt;FONT color="#FF0000"&gt;before&lt;/FONT&gt; DATE_EVENT or when REF_DATE is &lt;FONT color="#FF0000"&gt;after&lt;/FONT&gt; DATE_EVENT.&lt;/LI&gt;
&lt;LI&gt;In your rules describing separation of months into 3M, 6M, 9M, &lt;EM&gt;etc&lt;/EM&gt;., a literal reading of these rules seems like 3 months would get a FLAG value of BOTH 3M and 6M.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Ignoring these issues, and guessing at the answers about how to handle things, this is easily handled by the INTCK function and a custom format. Naturally if I have guessed wrong about how you want to handle things, you definitely ought to try fixing this code by yourself.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
    value monthf 0-3='3M' 4-6='6M' 7-9='9M' 10-12='12M' other='.';
run;
data db1;
    set db;
    delta_months=intck('month',ref_date,date_event);
    format delta_months monthf.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why use custom formats here? Because if you use custom formats, the 3M 6M 9M 12M sort properly. If you don't use custom formats, 12M sorts before 3M.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, thank you for providing rules rather than (or in addition to) examples, and thank you for providing example data in the desired format.&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 11:34:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987462#M43834</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-05-07T11:34:57Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987465#M43837</link>
      <description>Sorry, ddmm were inverted in Ref_date.  I edited accordingly.</description>
      <pubDate>Thu, 07 May 2026 12:11:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987465#M43837</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-07T12:11:11Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987466#M43838</link>
      <description>Sorry I edited. ddmm were inverted in Ref_date. I edited accordingly.</description>
      <pubDate>Thu, 07 May 2026 12:15:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987466#M43838</guid>
      <dc:creator>NewUsrStat</dc:creator>
      <dc:date>2026-05-07T12:15:53Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987467#M43839</link>
      <description>&lt;P&gt;I asked for clarification on 3 issues, you only explained the first one.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you are using informat&amp;nbsp;mmddyy. then row 7 and 8 don't make sense, it shows month number equal to 30. (PS: advice is always to test your code first, make sure it works and does what you want it to do before providing it to us; you will get better and faster answers if you provide us with debugged code)&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 12:29:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987467#M43839</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-05-07T12:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987468#M43840</link>
      <description>&lt;P&gt;Something like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. (Ref_date) (:mmddyy.) (Date_event) (:mmddyy.) Event :$20.;
  format Ref_date date9. Date_event date9.;
cards;
0001 09/01/2024  10/11/2024  Y
0001 09/01/2024  11/11/2024  Y
0001 09/01/2024       .      N
0001 09/01/2024  01/15/2025  Y
0002 09/11/2016  04/15/2016  Y
0003 30/06/2025  02/09/2025  Y
0003 30/06/2025  12/12/2025  Y
0004 12/03/2024       .      N
0005 12/03/2024  12/03/2024  Y
0005 12/03/2024  12/04/2024  Y
0006 12/03/2023  12/04/2024  Y
;
data db1;
    set db;
    length delta_months $ 3;
    if Event="Y" then delta_months=cats(3*(1&amp;lt;&amp;gt;abs(intck('month3',ref_date,date_event))),"M");
run;

proc print data=db1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 12:42:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987468#M43840</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2026-05-07T12:42:27Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987474#M43843</link>
      <description>&lt;P&gt;So assuming that by REF_DATE you mean the variable INDEX_DATE.&lt;/P&gt;
&lt;P&gt;Let's also correct your example to more closely match your problem description (and represent the dates in YMD order to avoid confusing the number of the month with the day of the month).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input ID :$20. (Index_date Date_event) (:yymmdd.) Event :$20. Flag :$20.;
  format Index_date Date_event yymmdd10.;
cards;
0001 2024-09-01 2024-10-11 Y 3M
0001 2024-09-01 2024-11-11 Y 3M
0001 2024-09-01 .          N . 
0001 2024-09-01 2025-01-15 Y 6M
0002 2016-09-11 2017-04-15 Y 9M
0003 2025-06-30 2025-02-09 Y . 
0003 2025-06-30 2025-12-12 Y 6M
0004 2024-12-03 .          N .
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So we can use the INTCK() function to check the number of month boundaries crossed. I have chosen to use the continuous method instead of testing for when it crosses day one of the month.&amp;nbsp; &amp;nbsp;But you also might want to use a fixed number of days instead to avoid the fact that calendar months are not all the same length.&lt;/P&gt;
&lt;P&gt;To convert from number of months to your 3,6,9,... values you can use the CEIL() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  after = (date_event &amp;gt; index_date);
  if after then diff = intck('month',index_date,date_event,'c');
  diff3 = 3*ceil(diff/3);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot 2026-05-07 at 9.22.50 AM.png" style="width: 782px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/114812i57F72C50AE247CE2/image-size/large?v=v2&amp;amp;px=999" role="button" title="Screenshot 2026-05-07 at 9.22.50 AM.png" alt="Screenshot 2026-05-07 at 9.22.50 AM.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 May 2026 13:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987474#M43843</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-05-07T13:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: Assign variable values to time intervals</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987553#M43847</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/134532"&gt;@NewUsrStat&lt;/a&gt;&amp;nbsp;, I think firstly there is something wrong with your raw data , probably it is the &lt;STRONG&gt;informat&lt;/STRONG&gt;,&amp;nbsp; &lt;STRONG&gt;column width&lt;/STRONG&gt;, maybe also the &lt;STRONG&gt;column type&lt;/STRONG&gt;, which are not properly set for the &lt;EM&gt;&lt;STRONG&gt;date &lt;/STRONG&gt;&lt;/EM&gt;columns and maybe also the &lt;EM&gt;&lt;STRONG&gt;ID &lt;/STRONG&gt;&lt;/EM&gt;column.&lt;SPAN&gt;&amp;nbsp;I copy and paste your code for the dateset, when print it, it does not display properly, regardless it is &lt;STRONG&gt;&lt;EM&gt;ddmmyy.&lt;/EM&gt;&lt;/STRONG&gt; or &lt;STRONG&gt;&lt;EM&gt;mmddyy.&lt;/EM&gt;&lt;/STRONG&gt; informat, the output of both were not correctly displayed (has missing values that was not supposed to be missing). The code, log, and output are as follows, just for your reference. So I suggest that the first thing is to modify the raw dataset to create a good one with right informat, format, width, and column type.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The other suggestion is that, when create that new column (I think it is something like event time to be further&amp;nbsp;used in, like, survival analysis), it is good practice to create two columns, one is number, the other is unit, for example, 3 months, the number column value is 3 and the unit column is month. So that when perform survival analysis you do not need to separate the character column (like what you create now) and convert it to number again, consumes a lot of time and labor.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data DB;
  input ID :$20. (Ref_date) (:mmddyy.) (Date_event) (:mmddyy.) Event :$20.;
  format Ref_date date9. Date_event date9.;
cards;
0001 09/01/2024  10/11/2024  Y
0001 09/01/2024  11/11/2024  Y
0001 09/01/2024       .      N
0001 09/01/2024  01/15/2025  Y
0002 09/11/2016  04/15/2016  Y
0003 30/06/2025  02/09/2025  Y
0003 30/06/2025  12/12/2025  Y
0004 12/03/2024       .      N
;
run;
proc print data=db;run;
data DB;
  input ID :$20. (Ref_date) (:ddmmyy.) (Date_event) (:ddmmyy.) Event :$20.;
  format Ref_date date9. Date_event date9.;
cards;
0001 09/01/2024  10/11/2024  Y
0001 09/01/2024  11/11/2024  Y
0001 09/01/2024       .      N
0001 09/01/2024  01/15/2025  Y
0002 09/11/2016  04/15/2016  Y
0003 30/06/2025  02/09/2025  Y
0003 30/06/2025  12/12/2025  Y
0004 12/03/2024       .      N
;
run;
proc print data=db;run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;69         data DB;
 70           input ID :$20. (Ref_date) (:mmddyy.) (Date_event) (:mmddyy.) Event :$20.;
 71           format Ref_date date9. Date_event date9.;
 72         cards;
 
 NOTE: Invalid data for Ref_date in line 78 6-15.
 RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0                     
 78         0003 30/06/2025  02/09/2025  Y
 ID=0003 Ref_date=. Date_event=09FEB2025 Event=Y _ERROR_=1 _N_=6
 NOTE: Invalid data for Ref_date in line 79 6-15.
 79         0003 30/06/2025  12/12/2025  Y
 ID=0003 Ref_date=. Date_event=12DEC2025 Event=Y _ERROR_=1 _N_=7&lt;/PRE&gt;
&lt;PRE&gt;84         data DB;
 85           input ID :$20. (Ref_date) (:ddmmyy.) (Date_event) (:ddmmyy.) Event :$20.;
 86           format Ref_date date9. Date_event date9.;
 87         cards;
 
 NOTE: Invalid data for Date_event in line 91 18-27.
 RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0                     
 91         0001 09/01/2024  01/15/2025  Y
 ID=0001 Ref_date=09JAN2024 Date_event=. Event=Y _ERROR_=1 _N_=4
 NOTE: Invalid data for Date_event in line 92 18-27.
 92         0002 09/11/2016  04/15/2016  Y
 ID=0002 Ref_date=09NOV2016 Date_event=. Event=Y _ERROR_=1 _N_=5&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1778221376900.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/114860i68154387ECB111D4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_1-1778221376900.png" alt="dxiao2017_1-1778221376900.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 08 May 2026 06:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Assign-variable-values-to-time-intervals/m-p/987553#M43847</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2026-05-08T06:41:03Z</dc:date>
    </item>
  </channel>
</rss>

