<?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: Creating new variables conditional on monyy. dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957780#M373865</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thanks for the quick response and the guidance! Changing my dates to '01FEB2010'd etc. worked for the 0/1 indicator values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Knowing that, do you have any guidance on how I could code a counter variable from a specific date onward?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 31 Jan 2025 10:50:42 GMT</pubDate>
    <dc:creator>sasgorilla</dc:creator>
    <dc:date>2025-01-31T10:50:42Z</dc:date>
    <item>
      <title>Creating new variables conditional on monyy. dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957778#M373863</link>
      <description>&lt;P&gt;I have a data set with a numeric month variable in monyy. format. I'm having trouble doing two things:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) I want to create indicator variables that are 0 until a certain month, after which they become 1. The below code resulted in the included errors for any months I used. I tried using 'FEB2010'd as well with same result for all months attempted.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;If month &amp;gt;= 'FEB10'd then x1=1; 
Else x1=0; 

/*Resulting Error*/
/*ERROR: Invalid date/time/datetime constant 'FEB10'd.
/*ERROR 77-185: Invalid number conversion on 'FEB10'd.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In my sample below I want x1 to become 1 from Feb2010 onward, and x2 to become 1 from Apr2010 onward.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) I also want to create variables that count months starting at certain months. In the below sample I have a variable I want to start counting upward each month by 1 beginning in Mar2010. The code I tried using in the example below was count1=intnx('month', 'MAR2010'd, 1), but I receive the same error as detailed above.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I feel like this is a simple solution, but I am having trouble figuring it out. I always struggle with dates. Thank you for your help!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data&amp;nbsp;have;
input&amp;nbsp;month&amp;nbsp;monyy.&amp;nbsp;exp&amp;nbsp;var_a;
format&amp;nbsp;month&amp;nbsp;monyy.;
datalines;
JAN10&amp;nbsp;1&amp;nbsp;100
JAN10&amp;nbsp;0&amp;nbsp;245
FEB10&amp;nbsp;1&amp;nbsp;75
FEB10&amp;nbsp;0&amp;nbsp;115
MAR10&amp;nbsp;1&amp;nbsp;100
MAR10&amp;nbsp;0&amp;nbsp;80
APR10 1 50
APR10 0 75
MAY10 1 100
MAY10 0 115
;
run;

data&amp;nbsp;have;
input&amp;nbsp;month&amp;nbsp;monyy.&amp;nbsp;exp&amp;nbsp;var_a x1 /*1 from FEB2010*/ x2 /*1 from Apr2010*/ count1 /*starts counting from Mar2010*/;
format&amp;nbsp;month&amp;nbsp;monyy.;
datalines;
JAN10&amp;nbsp;1&amp;nbsp;100 0 0 0
JAN10&amp;nbsp;0&amp;nbsp;245 0 0 0
FEB10&amp;nbsp;1&amp;nbsp;75 1 0 0
FEB10&amp;nbsp;0&amp;nbsp;115 1 0 0
MAR10&amp;nbsp;1&amp;nbsp;100 1 0 1
MAR10&amp;nbsp;0&amp;nbsp;80 1 0 1
APR10 1 50 1 1 2
APR10 0 75 1 1 2
MAY10 1 100 1 1 3
MAY10 0 115 1 1 3
; 
run;&amp;nbsp;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jan 2025 10:36:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957778#M373863</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-01-31T10:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables conditional on monyy. dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957779#M373864</link>
      <description>&lt;P&gt;Question 1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;'FEB10'd is not a valid SAS data literal. SAS does not understand this. Date literals MUST (as in, not optional, this is mandatory) be a two digit day followed by a 3 letter month, followed by a 2 or 4 digit year (and good practice means you will use a 4 digit year). No exceptions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So you can write '01FEB2010'd, SAS would understand this. But even so, I cannot yet write code for you, because I don't know what the variable MONTH contains. If it contains numeric SAS date values, representing the number of days since 01JAN1960, formatted in any way you want, then the code is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;If month &amp;gt;= '01FEB2010'd then x1=1; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, if the variable MONTH is character, or a numeric number which is not a valid SAS date value, such as 201002 (which could be interpreted by humans as February 2010 but would not be interpreted by SAS as February 2010), then I need specifics about what variable MONTH contains.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question 2: Same problem, same answer, SAS does not understand 'MAR2010'd.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2025 10:50:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957779#M373864</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-31T10:50:15Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables conditional on monyy. dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957780#M373865</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thanks for the quick response and the guidance! Changing my dates to '01FEB2010'd etc. worked for the 0/1 indicator values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Knowing that, do you have any guidance on how I could code a counter variable from a specific date onward?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2025 10:50:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957780#M373865</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-01-31T10:50:42Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables conditional on monyy. dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957782#M373866</link>
      <description>&lt;P&gt;Problem 2&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input month monyy. exp var_a x1 /*1 from FEB2010*/ x2 /*1 from Apr2010*/ ;
if month &amp;gt;= '01MAR2010'd then count1=intck('month','01MAR2010'd,month)+1; 
else count1=0;
format month monyy.;
datalines;
JAN10 1 100 0 0 0
JAN10 0 245 0 0 0
FEB10 1 75 1 0 0
FEB10 0 115 1 0 0
MAR10 1 100 1 0 1
MAR10 0 80 1 0 1
APR10 1 50 1 1 2
APR10 0 75 1 1 2
MAY10 1 100 1 1 3
MAY10 0 115 1 1 3
; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 31 Jan 2025 10:58:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957782#M373866</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-31T10:58:49Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables conditional on monyy. dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957783#M373867</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;Thank you! That worked perfectly. I have several counter variables I will apply it to and let you know if I have any other questions. I really appreciate your quick help!&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2025 11:03:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957783#M373867</guid>
      <dc:creator>sasgorilla</dc:creator>
      <dc:date>2025-01-31T11:03:25Z</dc:date>
    </item>
    <item>
      <title>Re: Creating new variables conditional on monyy. dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957800#M373871</link>
      <description>&lt;P&gt;Looks like you are trying to calculate three new variables.&amp;nbsp; Two boolean flags and one running count.&amp;nbsp; So you just need to two assignment statements and one sum statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by month;
  x1 = month &amp;gt;= '01FEB2010'd;
  x2 = month &amp;gt;= '01APR2010'd;
  count1 + first.month and (month &amp;gt;= '01MAR2010'd);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if the data is not sorted (or has missing months) then just use INTNX() to count the number of months.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;count1 = max(0,intck('month','01FEB2010'd,month));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 31 Jan 2025 15:05:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-new-variables-conditional-on-monyy-dates/m-p/957800#M373871</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-01-31T15:05:34Z</dc:date>
    </item>
  </channel>
</rss>

