<?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: Create character macro from todays date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570166#M160766</link>
    <description>&lt;P&gt;Maxim 28: Macro Variables Need no Formats.&lt;/P&gt;
&lt;P&gt;Put the raw value into the macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('fra_dato',today()-60);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then use it as is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;FactDt &amp;gt; &amp;amp;fra_dato&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's a given, of course, that dates should always be stored as such (numeric with a date format assigned). You only need 4 bytes instead of the 10 characters, and you can use all the SAS tools available for dates (functions, formats).&lt;/P&gt;
&lt;P&gt;In your case this means use of the intnx() function, which is much better to calculate "2 months back" than the overly simplistic subtraction of 60 days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need dashes in a displayed date, use the yymmddd10. format (note the third "d").&lt;/P&gt;</description>
    <pubDate>Mon, 01 Jul 2019 09:35:49 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-07-01T09:35:49Z</dc:date>
    <item>
      <title>Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570156#M160756</link>
      <description>&lt;P&gt;Hi!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I use SAS 9.4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a query that gives me a output with observations after a given date. This date is always two months ago. I want to create a macro that automaticly gives me all observations within the last two months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem is that the variable that i run this query against, for example: "...and factdt&amp;gt;'2019-04-27' ", is a character-variable and not a date-variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to create a character-macro from the date here:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;dato = sum(today()-60);&lt;BR /&gt;DATENEW=PUT(dato,yymmdd10.);&lt;BR /&gt;call symput('fra_dato', DATENEW);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;and have putted it into the query like this :&amp;nbsp;and FactDt&amp;gt;'&amp;amp;fra_dato'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This gives me the following error-message:&lt;/P&gt;&lt;P&gt;ERROR: Open cursor error: ICommand::Execute failed. : Conversion failed when converting date&lt;BR /&gt;and/or time from character string.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What am I'm doing wrong?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 08:42:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570156#M160756</guid>
      <dc:creator>wixol87</dc:creator>
      <dc:date>2019-07-01T08:42:18Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570158#M160758</link>
      <description>&lt;P&gt;Welcome to the SAS Community &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't see your data, so needless to say, this code is untested. However, you can do something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
    set have;
    where input(factdt, yymmdd10.) ge intnx('month', today(), -2, 's');
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jul 2019 08:45:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570158#M160758</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-07-01T08:45:53Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570161#M160761</link>
      <description>&lt;P&gt;Thank you! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The factdt is part of a SQL-query, here is the rest of the where-statement:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;where b.custbranchorgid='xxx' and b.isrecent=1 and FactDt&amp;gt;'2019-04-27' and a.CrmOpportunityPhaseCd=100&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope this is what you asked for?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can't see how I can use your answer in this query&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 08:55:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570161#M160761</guid>
      <dc:creator>wixol87</dc:creator>
      <dc:date>2019-07-01T08:55:11Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570162#M160762</link>
      <description>&lt;P&gt;I would think that this Where Clause gives you what you want&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where b.custbranchorgid='xxx' and b.isrecent=1 and input(factdt, yymmdd10.) gt intnx('month', today(), -2, 's') and a.CrmOpportunityPhaseCd=100&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jul 2019 09:01:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570162#M160762</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-07-01T09:01:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570166#M160766</link>
      <description>&lt;P&gt;Maxim 28: Macro Variables Need no Formats.&lt;/P&gt;
&lt;P&gt;Put the raw value into the macro variable:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('fra_dato',today()-60);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;then use it as is:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;FactDt &amp;gt; &amp;amp;fra_dato&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's a given, of course, that dates should always be stored as such (numeric with a date format assigned). You only need 4 bytes instead of the 10 characters, and you can use all the SAS tools available for dates (functions, formats).&lt;/P&gt;
&lt;P&gt;In your case this means use of the intnx() function, which is much better to calculate "2 months back" than the overly simplistic subtraction of 60 days.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you need dashes in a displayed date, use the yymmddd10. format (note the third "d").&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 09:35:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570166#M160766</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-01T09:35:49Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570200#M160773</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If source variable&amp;nbsp;&lt;SPAN&gt;factdt is really character as the OP states then I believe&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31304"&gt;@PeterClemmensen&lt;/a&gt;'s suggestion is what's going to work.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 12:24:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570200#M160773</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-07-01T12:24:44Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570217#M160778</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/279973"&gt;@wixol87&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Thank you! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The factdt is part of a SQL-query, here is the rest of the where-statement:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;where b.custbranchorgid='xxx' and b.isrecent=1 and FactDt&amp;gt;'2019-04-27' and a.CrmOpportunityPhaseCd=100&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this is what you asked for?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I can't see how I can use your answer in this query&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The SAS macro processor does not modify text inside of single quotes.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is the WHERE clause part of the SAS code?&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;FactDt&amp;gt;"&amp;amp;fra_dato"&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or are you using explicitly sending some SQL to a remote database that only allows sting literals to be enclosed in single quotes?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;... from connection to ... 
( ... FactDt&amp;gt; %bquote('&amp;amp;fra_dato') ...
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jul 2019 12:57:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570217#M160778</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-01T12:57:22Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570484#M160870</link>
      <description>&lt;P&gt;Hi all!&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Sorry for my late answer. I've been quite busy now before the summer vacation. Thanks for all your answers.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found a solution that's working by using some code I found in a program, but i don't know why this is working and not my earlier tries&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, here is what I did:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data test;&lt;BR /&gt;dato = sum(today()-65);&lt;BR /&gt;DATECRM=PUT(dato,yymmdd10.);&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql noprint;&lt;BR /&gt;select distinct "'"||strip(DATECRM)||"'" into :DATECRM separated by ' ' from test;&lt;BR /&gt;quit;&lt;BR /&gt;%put &amp;amp;DATECRM.;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;connect to oledb as db_edw (provider=sqloledb properties=(&lt;BR /&gt;"Data Source" = "LHPM-SQL-PROD"&lt;BR /&gt;"Initial Catalog" = "LHPMMART"&lt;BR /&gt;"Integrated Security" = "SSPI"&lt;BR /&gt;"Persist Security Info" = "False") BCP=YES) ;&lt;BR /&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;&lt;P&gt;where b.custbranchorgid='xxxx' and b.isrecent=1 and factdt&amp;gt;&amp;amp;DATECRM. and a.CrmOpportunityPhaseCd=100&lt;BR /&gt;);&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;I can't see who wrote it, but I have as you can see a connection.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 10:52:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570484#M160870</guid>
      <dc:creator>wixol87</dc:creator>
      <dc:date>2019-07-02T10:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570494#M160875</link>
      <description>&lt;P&gt;This:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
dato = sum(today()-65);
DATECRM=PUT(dato,yymmdd10.);
run;

 

proc sql noprint;
select distinct "'"||strip(DATECRM)||"'" into :DATECRM separated by ' ' from test;
quit;
%put &amp;amp;DATECRM.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;is equivalent to&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
call symputx('datecrm',cats("'",put(today()-65,yymmdd10.),"'"));
run;
%put &amp;amp;datecrm.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The data _null_ is only there to make use of single quotes easier.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have now moved the single quotes &lt;EM&gt;into&lt;/EM&gt; the macro variable, so you do not need them where they would prevent macro variable resolution. Sometimes it &lt;EM&gt;is&lt;/EM&gt; important to have quotes and formatted values in macro variables (exception to Maxim 28).&lt;/P&gt;</description>
      <pubDate>Tue, 02 Jul 2019 11:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570494#M160875</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-02T11:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: Create character macro from todays date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570518#M160881</link>
      <description>&lt;P&gt;There are two reasons why you might need to use single quotes instead of double quotes around a value.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;You are pushing the code into a remote database that does not recognize string in double quotes as string literals. (This seems to be your situation.)&lt;/LI&gt;
&lt;LI&gt;You want to prevent the macro processor from evaluating macro triggers ( &amp;amp; and % ) in the string.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;If you frequently need to add single quotes around macro variable values (or other macro code expressions) then I have found it useful to have an autocall macro for that.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://raw.githubusercontent.com/sasutils/macros/master/squote.sas" target="_blank"&gt;https://raw.githubusercontent.com/sasutils/macros/master/squote.sas&lt;/A&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let DATECRM=%squote(%sysfunc(putn("&amp;amp;sysdate9"d-65,yymmdd10)));
...
 factdt&amp;gt;&amp;amp;datecrm&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jul 2019 12:31:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-character-macro-from-todays-date/m-p/570518#M160881</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-02T12:31:01Z</dc:date>
    </item>
  </channel>
</rss>

