<?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: Macro for character_date range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768495#M243765</link>
    <description>&lt;P&gt;Yes, text string such as '2021_01' will work if your only interest is sorting or boolean comparisons. Actual numeric SAS dates still work better if you want to do arithmetic (such as, what month was 18 months ago?) and works just as well as text strings for sorting or boolean comparisons. So, actual numeric SAS dates work in all situations; text strings do not. Thus I would recommend using actual numeric SAS dates.&lt;/P&gt;</description>
    <pubDate>Sun, 19 Sep 2021 22:24:49 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2021-09-19T22:24:49Z</dc:date>
    <item>
      <title>Macro for character_date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768461#M243756</link>
      <description>&lt;P&gt;I have a dataset that have two variables, period (character) and sales. The time series starts in Januari 2021 and grows every month when a new month’s salesfigures is added. &lt;BR /&gt;The variable period contains information about month and year. Even months that have not yet occurred are in the dataset.&lt;/P&gt;
&lt;P&gt;I would like to create a macro so I can select periods that have occurred. That is possible to update very month.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have tried this:&lt;/P&gt;
&lt;P&gt;%Let t_period='Apr_2021' &amp;lt;=period&amp;lt;= 'Sep_2021';&lt;/P&gt;
&lt;P&gt;Not sure if I am doing it right.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;;&lt;BR /&gt;input period$ sales;&lt;BR /&gt;datalines;&lt;BR /&gt;Jan_2021 100&lt;BR /&gt;Feb_2021 200&lt;BR /&gt;Mar_2021 150&lt;BR /&gt;Apr_2021 320&lt;BR /&gt;May_2021 220&lt;BR /&gt;Jun_2021 250&lt;BR /&gt;Jul_2021 320&lt;BR /&gt;Aug_2021 400&lt;BR /&gt;Sep_2021 550&lt;BR /&gt;Oct_2021 .&lt;BR /&gt;Nov_2021 .&lt;BR /&gt;Dec_2021 .&lt;BR /&gt;Jan_2022 .&lt;BR /&gt;Feb_2022 .&lt;BR /&gt;Mar_2022 .&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data want;&lt;BR /&gt;;&lt;BR /&gt;input Period$ Sales;&lt;BR /&gt;datalines;&lt;BR /&gt;Jan_2021 100&lt;BR /&gt;Feb_2021 200&lt;BR /&gt;Mar_2021 150&lt;BR /&gt;Apr_2021 320&lt;BR /&gt;May_2021 220&lt;BR /&gt;Jun_2021 250&lt;BR /&gt;Jul_2021 320&lt;BR /&gt;Aug_2021 400&lt;BR /&gt;Sep_2021 550&lt;BR /&gt;;&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;Thanks&lt;/P&gt;</description>
      <pubDate>Sun, 19 Sep 2021 14:17:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768461#M243756</guid>
      <dc:creator>Chris_LK_87</dc:creator>
      <dc:date>2021-09-19T14:17:39Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for character_date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768465#M243757</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Never use CHAR values for your date(time)s!&lt;/P&gt;
&lt;P&gt;You are unable to use comparison operators then (as well as useful functions like INTCK and INTNX and many others).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;See below!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input period $ sales;
datalines;
Jan_2021 100
Feb_2021 200
Mar_2021 150
Apr_2021 320
May_2021 220
Jun_2021 250
Jul_2021 320
Aug_2021 400
Sep_2021 550
Oct_2021 .
Nov_2021 .
Dec_2021 .
Jan_2022 .
Feb_2022 .
Mar_2022 .
;
run;

data want;
 set have;
 ProperDateTemp='01'!!scan(period,1,'_')!!scan(period,2,'_');
 ProperDateNume=input(ProperDateTemp,date9.);
 ProperDateNume=INTNX('MONTH',ProperDateNume,0,'END');
 if ProperDateNume &amp;lt; today() then output;
 format ProperDateNume date9.; /* You can also try MONYY7. to avoid days to be displayed */
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I transform your char value to the last day of the month, using a proper type (numeric) and (in)format (date9.).&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Sun, 19 Sep 2021 14:41:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768465#M243757</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-09-19T14:41:19Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for character_date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768466#M243758</link>
      <description>&lt;P&gt;Convert the string into a DATE to test.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let t_period=
  '01APR2021'd &amp;lt;= input(cats('01',period),date11.) &amp;lt;= '01SEP2021'd
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want the boundaries to be dynamic is %SYSFUNC() and INTNX().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let t_period=
  %sysfunc(intnx(month,%sysfunc(date()),-5))
 &amp;lt;= input(cats('01',period),date11.) 
 &amp;lt;= %sysfunc(intnx(month,%sysfunc(date()),0))
;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 19 Sep 2021 15:10:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768466#M243758</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-09-19T15:10:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for character_date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768471#M243760</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I have tried this:&lt;/P&gt;
&lt;P&gt;%Let t_period='Apr_2021' &amp;lt;=period&amp;lt;= 'Sep_2021';&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As others have said, using character strings to represent calendar values makes the process incredibly difficult, if not impossible. The code above will also find Nov_2021 (and Nov_2020) as between 'Apr_2021' and 'Sep_2021'. Why? Because character strings are evaluated alphabetically, and alphabetically Nov is between Apr and Sep.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As other have provided code where your dates are represented as numbers instead of character strings, I don't need to provide code here.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Sep 2021 16:22:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768471#M243760</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-19T16:22:41Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for character_date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768480#M243762</link>
      <description>&lt;P&gt;Is it too late to change your data?&amp;nbsp; As others have shown, it's a somewhat complex problem with data in its current form.&amp;nbsp; However, it could be a simple problem.&amp;nbsp; Change from:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Jan_2021
Feb_2021
Mar_2021&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Instead, use:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;2021_01
2021_02
2021_03&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the comparisons will be extremely simple.&amp;nbsp; As an added bonus, sorting the data properly becomes much easier.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Sep 2021 18:42:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768480#M243762</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2021-09-19T18:42:28Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for character_date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768495#M243765</link>
      <description>&lt;P&gt;Yes, text string such as '2021_01' will work if your only interest is sorting or boolean comparisons. Actual numeric SAS dates still work better if you want to do arithmetic (such as, what month was 18 months ago?) and works just as well as text strings for sorting or boolean comparisons. So, actual numeric SAS dates work in all situations; text strings do not. Thus I would recommend using actual numeric SAS dates.&lt;/P&gt;</description>
      <pubDate>Sun, 19 Sep 2021 22:24:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768495#M243765</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-09-19T22:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: Macro for character_date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768721#M243857</link>
      <description>&lt;P&gt;On top of what the others already said, a SAS date can be stored in just 4 bytes of data (use&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length date 4;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;), so you need only half the place of your current strings; with lots of dates in your data, this means less I/O and therefore speedier programs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do this first:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data better;
set have (rename=(period=period_char));
length period 4;
period = input(period_char,monyy8.);
format period yymmd7.;
drop period_char;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then the WHERE is easy and works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set better;
where '01Apr2021'd &amp;lt;= period &amp;lt;= '01Sep2021'd;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Sep 2021 09:41:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-for-character-date-range/m-p/768721#M243857</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-09-21T09:41:31Z</dc:date>
    </item>
  </channel>
</rss>

