<?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: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502200#M134028</link>
    <description>&lt;P&gt;Good point&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;.&amp;nbsp; I was surprised to see that&amp;nbsp;ANYDTDTE informat doesn't actually generate invalid data errors:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;68   data _null_ ;
69     date=input("foo", ANYDTDTE60.) ;
70     put date= ;
71   run ;

date=.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Found this usage note that describes it:&amp;nbsp; &lt;A href="http://support.sas.com/kb/14/291.html" target="_blank"&gt;http://support.sas.com/kb/14/291.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does that fact that it's a usage note rather than a problem note mean that this is an intended feature?&amp;nbsp; Or since the table does show a 'fixed' column with null values, does that suggest it could be fixed in a future version, so that invalid values would throw an error?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I typically stay away from anydtdte, as it seems like more guessing than I would want.&amp;nbsp; But I suppose if one is willing to take some mess of differently formatted date strings and push it through anydtdte, accepting its interpretation of the strings, they may not want errors for any values where anydtdte guesses that it's not a date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 06 Oct 2018 22:50:19 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2018-10-06T22:50:19Z</dc:date>
    <item>
      <title>SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/501849#M133862</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have made a couple of macros that makes life much easier when you have to deal with dates in macro variables. The examples illustrate the use from within a macro.&amp;nbsp;Please feel free to put them in your SAS Macro Library for easy use.&lt;/P&gt;
&lt;P&gt;Have a nice weekend &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;isValidDate&lt;/STRONG&gt;&lt;BR /&gt;Returns True (1) if the date is valid and False (0) if not.&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;%macro isValidDate(date);
    %local rc datevalidation;
    %let rc=%sysfunc(dosubl(%str(data _null_;date=input("&amp;amp;date", ?? ANYDTDTE60.);call symputx("datevalidation",date);run;)));
    %if &amp;amp;datevalidation=. %then %do;
        0
        %return;
    %end;
    %else %do;
        1
        %return;
    %end;
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example&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;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date=2018-10-33;

%if not %isValidDate(&amp;amp;date) %then %do;
%put ERROR: DATE is not a valid date!;
%return;
%end;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;toDate&lt;/STRONG&gt;&lt;BR /&gt;Converts a text date to a SAS date. Can be used when comparing two dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro toDate(date);
    %local rc datevalidation;
    %let rc=%sysfunc(dosubl(%str(data _null_;date=input("&amp;amp;date", ?? ANYDTDTE60.);call symputx("datevalidation",date);run;)));
    &amp;amp;datevalidation
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let date_depart=2018-12-05;
%let date_arrival=2018-10-07;

%if %toDate(&amp;amp;date_depart) &amp;gt; %toDate(&amp;amp;date_arrival) %then %do;
    %put ERROR: Departure date after arrival date!;
    %return;
%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;STRONG&gt;toDay&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;Gives you todays date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro toDay;
    %sysfunc(today())
%mend;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let registrationdate=2018-10-07;

%if %toDate(&amp;amp;registrationdate) &amp;gt; %toDay %then %do;
    %put ERROR: Registration date after today!;
    %return;
%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Oct 2018 12:01:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/501849#M133862</guid>
      <dc:creator>PeterKellberg</dc:creator>
      <dc:date>2018-10-05T12:01:54Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502071#M133968</link>
      <description>&lt;P&gt;DOSUBL is quite a hammer, but I do not&amp;nbsp;see this task as a nail.&amp;nbsp;&amp;nbsp; The datevaliddation macro can be done without invoking a data step, which would surely be more expensive than embedding an INPUTN function and IFN function&amp;nbsp;in&amp;nbsp;nested %sysfunc's.&amp;nbsp;&amp;nbsp; It would be difficult for&amp;nbsp; me to recommend&amp;nbsp; DOSUBL-dependent approaches when other techniques are readily available.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro valdate(date);
  %local date;
  %sysfunc(ifn(%sysfunc(inputn(&amp;amp;date,ANYDTDTE60.))=.,0,1))
%mend;

%let rslt=%valdate(41jan2011);
%put &amp;amp;=rslt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The same approach could be used for the TODATE macro.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Oct 2018 23:14:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502071#M133968</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-10-05T23:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502093#M133977</link>
      <description>&lt;P&gt;Thanks for your thoughts &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; I know that we dont want a datastep when not needed and especially not in "Boolean" macros. That is&amp;nbsp; why I used DOSUBL. And why datastep? Because the ?? (to suppress warnings if invalid date - I dont want them) in the INPUT function simply is not available in the INPUTN function. I cant wait until monday to try your solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards, Peter&lt;/P&gt;</description>
      <pubDate>Sat, 06 Oct 2018 07:56:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502093#M133977</guid>
      <dc:creator>PeterKellberg</dc:creator>
      <dc:date>2018-10-06T07:56:32Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502112#M133987</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/17589"&gt;@PeterKellberg&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I revised your macro by removing the ??, and used it against an invalid date (2018-13-33), and get no warning message, (and same in my alternative), even though it does produce a warning when run in normal code.&amp;nbsp;&amp;nbsp;Are you sure you need the "??" inside the macro?&amp;nbsp;&amp;nbsp;&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>Sat, 06 Oct 2018 13:20:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502112#M133987</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2018-10-06T13:20:02Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502148#M134012</link>
      <description>&lt;P&gt;You cannot use INPUT()&amp;nbsp; with %SYSFUNC(), but you can use INPUTN() and INPUTC() instead.&lt;/P&gt;
&lt;P&gt;I always assumed it just made it easier to implement for %SYSFUNC() to know whether to treat the&amp;nbsp;result as string or a number.&lt;/P&gt;</description>
      <pubDate>Sat, 06 Oct 2018 17:07:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502148#M134012</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-10-06T17:07:19Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502200#M134028</link>
      <description>&lt;P&gt;Good point&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;.&amp;nbsp; I was surprised to see that&amp;nbsp;ANYDTDTE informat doesn't actually generate invalid data errors:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;68   data _null_ ;
69     date=input("foo", ANYDTDTE60.) ;
70     put date= ;
71   run ;

date=.
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Found this usage note that describes it:&amp;nbsp; &lt;A href="http://support.sas.com/kb/14/291.html" target="_blank"&gt;http://support.sas.com/kb/14/291.html&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does that fact that it's a usage note rather than a problem note mean that this is an intended feature?&amp;nbsp; Or since the table does show a 'fixed' column with null values, does that suggest it could be fixed in a future version, so that invalid values would throw an error?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I typically stay away from anydtdte, as it seems like more guessing than I would want.&amp;nbsp; But I suppose if one is willing to take some mess of differently formatted date strings and push it through anydtdte, accepting its interpretation of the strings, they may not want errors for any values where anydtdte guesses that it's not a date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 06 Oct 2018 22:50:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502200#M134028</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-10-06T22:50:19Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502332#M134086</link>
      <description>&lt;P&gt;Thanks for the supreme input from you!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What did I learn? The ANYDTDTE informat does not produce messages in the log like this (as do any other DATE informat - I presume).&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;NOTE: Invalid argument to function INPUT at line 7 column 10.
date=. _ERROR_=1 _N_=1
NOTE: Mathematical operations could not be performed at the following places. The results of the operations have been set to missing values.
      Each place is given by: (Number of times) at (Line):(Column).
      1 at 7:10
&lt;/PRE&gt;
&lt;P&gt;So if we choose the&amp;nbsp;&lt;SPAN&gt;ANYDTDTE informat for checking a date for being valid we can use the INPUTN function straight away:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro isValidDate(date);
    %if %sysfunc(inputn(&amp;amp;date,ANYDTDTE10.)) = . %then
        0;
    %else
        1;
%mend;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;or more compact&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro isValidDate(date);
    %sysfunc(ifn(%sysfunc(inputn(&amp;amp;date,anydtdte10.)),1,0))
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;As mentioned the ANYDTDTE informat may not be the favorite - we want perhaps to use the yymmdd informat:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;/* input registration date on the form yyyy-mm-dd */
%let regdate=2018-10-08;
&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Because the yymmdd informat will produce annoying messages in the log when it encounters an invalid date AND ?? to suppress those messages is not available in the INPUTN function - the solution is a DATA step that can make use of the INPUT function.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;We cannot just have that DATA step inside the macro because the code is passed back to the Input Stack. The whole point in this is to make a macro that only returns TRUE/FALSE. Ordinary code will ruin this approach. Somehow the DOSUBL function does not do that. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;So thats why I chose that solution. A variant in order to supply your choice of DATE informat:&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro isValidDate(date,informat);
    %local rc datevalidation;
    %let rc=%sysfunc(dosubl(%str(data _null_;date=input("&amp;amp;date", ?? &amp;amp;informat);call symputx("datevalidation",date);run;)));
    %if &amp;amp;datevalidation=. %then
        0;
    %else
        1;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;And the testing will look like this&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if not %isValidDate(&amp;amp;date,yymmdd10.) %then %do;
    %put ERROR: The date is invalid. Enter a valid date in the format yyyy-mm-dd.;
    %return;
%end;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Peter&lt;/SPAN&gt;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 11:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502332#M134086</guid>
      <dc:creator>PeterKellberg</dc:creator>
      <dc:date>2018-10-08T11:02:39Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502347#M134093</link>
      <description>&lt;P&gt;Nice.&amp;nbsp; So as a general point, %SYSFUNC allows you to call almost all SAS functions from the macro language.&amp;nbsp; For functions that cannot be called from the macro language, you can wrap them in a function-style macro with a DOSUBL block called by %SYSFUNC.&amp;nbsp; For those wanting to read more about such macro functions, see Rick Langston's excellent paper:&amp;nbsp;&lt;A href="https://support.sas.com/resources/papers/proceedings13/032-2013.pdf.&amp;nbsp;" target="_blank"&gt;https://support.sas.com/resources/papers/proceedings13/032-2013.pdf.&amp;nbsp;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Oct 2018 11:17:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502347#M134093</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2018-10-08T11:17:26Z</dc:date>
    </item>
    <item>
      <title>Re: SAS Macro Programming: A couple of SAS Macros to validate dates in macro variables.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502350#M134095</link>
      <description>Thx Quentin &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt; Hat off for Mr. Rick. Again I learned something new: PROC STREAM.</description>
      <pubDate>Mon, 08 Oct 2018 11:36:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/SAS-Macro-Programming-A-couple-of-SAS-Macros-to-validate-dates/m-p/502350#M134095</guid>
      <dc:creator>PeterKellberg</dc:creator>
      <dc:date>2018-10-08T11:36:32Z</dc:date>
    </item>
  </channel>
</rss>

