<?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: Formatting date variable and creating a macro variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962545#M375154</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209976"&gt;@donspaul&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need help with processing a date variable that I need to pass to a macro variable.&amp;nbsp; The date variable is in the following format&amp;nbsp;20MAR2025:00:00:00.000000 (DATETIME25.6).&amp;nbsp; I need help with accomplishing 2 things.&amp;nbsp; First, I need to reformat this date variable like so&amp;nbsp;2025-03-20 00:00:00.&amp;nbsp; Second, I need to pass&amp;nbsp;"2025-03-20 00:00:00" into a macro variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you mean you have NUMERIC&amp;nbsp; variable that has the DATETIME25.6 format attached to it so the values print in that character pattern you showed?&amp;nbsp; Or do you have CHARACTER variable that has strings that match the pattern that the DATETIME25.6 format will generate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a datetime value you want to generate a string like that into a macro variable you could generate it in two pieces.&amp;nbsp; Say your existing variable is numeric and is named DT and it is in a single observation dataset named HAVE.&amp;nbsp; You could use this step to make a macro variable named DTSTRING with the string you showed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  call symputx('dtstring',put(dt,dtdate9.)||' '||put(dt,tod8.));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;1    data _null_;
2      dt = '20MAR2025:00:00:00.000000'dt;
3      call symputx('dtstring',put(dt,dtdate9.)||' '||put(dt,tod8.));
4    run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


5
6    %put |&amp;amp;dtstring|;
|20MAR2025 00:00:00|&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 24 Mar 2025 19:41:36 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-03-24T19:41:36Z</dc:date>
    <item>
      <title>Formatting date variable and creating a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962536#M375146</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need help with processing a date variable that I need to pass to a macro variable.&amp;nbsp; The date variable is in the following format&amp;nbsp;20MAR2025:00:00:00.000000 (DATETIME25.6).&amp;nbsp; I need help with accomplishing 2 things.&amp;nbsp; First, I need to reformat this date variable like so&amp;nbsp;2025-03-20 00:00:00.&amp;nbsp; Second, I need to pass&amp;nbsp;"2025-03-20 00:00:00" into a macro variable.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 18:30:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962536#M375146</guid>
      <dc:creator>donspaul</dc:creator>
      <dc:date>2025-03-24T18:30:54Z</dc:date>
    </item>
    <item>
      <title>Re: Formatting date variable and creating a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962539#M375148</link>
      <description>&lt;P&gt;Most of the time, you do not want formatted macro variables. You want unformatted macro variables. See&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/Maxims-of-Maximally-Efficient-SAS-Programmers/ta-p/352068" target="_self"&gt;Maxim 28&lt;/A&gt;. (Yes, there are exceptions, so please tell us what you will do with this macro variable in the programming you are doing)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, an unformatted macro variable can be obtained thusly:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    datetime='20MAR2025:00:00:00.000000'dt;
    call symputx('datetime',datetime);
run;

%put &amp;amp;=datetime; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;and it can be used unformatted for any mathematical calculations (such as INTNX or INTCK or others) and for any logical comparisons (such as &lt;FONT face="courier new,courier"&gt;if dt&amp;lt;&amp;amp;datetime&lt;/FONT&gt; where dt is a data set variable which contains datetime values).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 19:17:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962539#M375148</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-24T19:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: Formatting date variable and creating a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962544#M375153</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for weighing in, but unfortunately, I am still having issues.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to set up an automation that will work like this: (1) identify the maximum date in a data table (end date), (2) use INTNX to capture the 12-month prior date (start date), (3) pass the start-date and the end-date to 2 separate macro variables, and (4) set the start-date and end-date (macro variables) in a WHERE clause so it returns rows only within these 2-dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I manually set the macro variable as follows, the query works just fine:&lt;/P&gt;&lt;P&gt;%let start_dt = '2020-12-31 00:00:00';&lt;BR /&gt;%let end_dt = '2024-01-01 00:00:00';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, when I used the following code, the query is not returning any rows.&amp;nbsp; Because the only difference is in how the date fields are being passed to the query, I am suspecting the macro variable is not resolving properly.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT END_DATE FORMAT=BEST32.&lt;BR /&gt;INTO: END_DATE&lt;BR /&gt;FROM DATA_DATE_END;&lt;BR /&gt;QUIT;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having the same issue when I used the code you had shared below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please let me know if you have any further questions.&amp;nbsp; I don't know if this is material or not, but I am using Enterprise Guide to query a table in RedShift using an ODBC connection.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 19:40:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962544#M375153</guid>
      <dc:creator>donspaul</dc:creator>
      <dc:date>2025-03-24T19:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: Formatting date variable and creating a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962545#M375154</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209976"&gt;@donspaul&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need help with processing a date variable that I need to pass to a macro variable.&amp;nbsp; The date variable is in the following format&amp;nbsp;20MAR2025:00:00:00.000000 (DATETIME25.6).&amp;nbsp; I need help with accomplishing 2 things.&amp;nbsp; First, I need to reformat this date variable like so&amp;nbsp;2025-03-20 00:00:00.&amp;nbsp; Second, I need to pass&amp;nbsp;"2025-03-20 00:00:00" into a macro variable.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you mean you have NUMERIC&amp;nbsp; variable that has the DATETIME25.6 format attached to it so the values print in that character pattern you showed?&amp;nbsp; Or do you have CHARACTER variable that has strings that match the pattern that the DATETIME25.6 format will generate.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have a datetime value you want to generate a string like that into a macro variable you could generate it in two pieces.&amp;nbsp; Say your existing variable is numeric and is named DT and it is in a single observation dataset named HAVE.&amp;nbsp; You could use this step to make a macro variable named DTSTRING with the string you showed.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set have;
  call symputx('dtstring',put(dt,dtdate9.)||' '||put(dt,tod8.));
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;PRE&gt;1    data _null_;
2      dt = '20MAR2025:00:00:00.000000'dt;
3      call symputx('dtstring',put(dt,dtdate9.)||' '||put(dt,tod8.));
4    run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


5
6    %put |&amp;amp;dtstring|;
|20MAR2025 00:00:00|&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 19:41:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962545#M375154</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-24T19:41:36Z</dc:date>
    </item>
    <item>
      <title>Re: Formatting date variable and creating a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962546#M375155</link>
      <description>&lt;P&gt;So it sounds like you are using pass thru SQL so that you want to use the macro variable to generate REDSHIFT SQL syntax, not SAS syntax.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use the QUOTE() function to add those single quotes around the value.&lt;/P&gt;
&lt;PRE&gt;1    data _null_;
2      dt = '20MAR2025:00:00:00.000000'dt;
3      call symputx('dtstring',quote(put(dt,dtdate9.)||' '||put(dt,tod8.),"'"));
4    run;

NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


5
6    %put &amp;amp;=dtstring;
DTSTRING='20MAR2025 00:00:00'
&lt;/PRE&gt;</description>
      <pubDate>Mon, 24 Mar 2025 19:47:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962546#M375155</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-24T19:47:08Z</dc:date>
    </item>
    <item>
      <title>Re: Formatting date variable and creating a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962547#M375156</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/209976"&gt;@donspaul&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you for weighing in, but unfortunately, I am still having issues.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to set up an automation that will work like this: (1) identify the maximum date in a data table (end date), (2) use INTNX to capture the 12-month prior date (start date), (3) pass the start-date and the end-date to 2 separate macro variables, and (4) set the start-date and end-date (macro variables) in a WHERE clause so it returns rows only within these 2-dates.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When I manually set the macro variable as follows, the query works just fine:&lt;/P&gt;
&lt;P&gt;%let start_dt = '2020-12-31 00:00:00';&lt;BR /&gt;%let end_dt = '2024-01-01 00:00:00';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, when I used the following code, the query is not returning any rows.&amp;nbsp; Because the only difference is in how the date fields are being passed to the query, I am suspecting the macro variable is not resolving properly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PROC SQL;&lt;BR /&gt;SELECT END_DATE FORMAT=BEST32.&lt;BR /&gt;INTO: END_DATE&lt;BR /&gt;FROM DATA_DATE_END;&lt;BR /&gt;QUIT;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am having the same issue when I used the code you had shared below.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please let me know if you have any further questions.&amp;nbsp; I don't know if this is material or not, but I am using Enterprise Guide to query a table in RedShift using an ODBC connection.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thank you for explaining, there's no way anyone can give valid advice without these details. You want &amp;amp;END_DATE to be unformatted. You want &amp;amp;START_DATE to be unformatted. Then everything works, assuming your date variable is NUMERIC date variable and has any format you want or even if it is unformatted.&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;PROC SQL;
    SELECT END_DATE FORMAT=BEST32.
    INTO: END_DATE
    FROM DATA_DATE_END;
QUIT;
/* Step 2 */
data _null_;
     set have;
     /* NOTE WE USE UNFORMATTED &amp;amp;END_DATE */
     start_date=intnx('dtmonth',&amp;amp;end_date,-12,'b');
     /* NOTE &amp;amp;START_DATE is unformatted */
     call symputx('start_date',start_date);
run;
/* Step 3 */
data want;
    /* NOTE VARIABLES CAN BE FORMATTED IN THE DATA SET */
    set yourbigdataset;
    /* NOTE THIS WORKS WITH UNFORMATTED MACRO VARIABLES */
    if datevariable = &amp;amp;start_date or date variable=datepart(&amp;amp;end_date) then output;
run;    

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If datevariable is actually a datetime variable, we can modify the code to work with that as well.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 20:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962547#M375156</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-24T20:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: Formatting date variable and creating a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962554#M375160</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;I don't know if this is material or not, but I am using Enterprise Guide to query a table in RedShift using an ODBC connection.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, yes, it depends on what you are doing and again, more details are needed. What date or datetime values do you need in this ODBC connection? If you were to type in a specific date, what would this WHERE statement look like? Please be exact.&lt;/P&gt;</description>
      <pubDate>Mon, 24 Mar 2025 20:47:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962554#M375160</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-24T20:47:56Z</dc:date>
    </item>
    <item>
      <title>Re: Formatting date variable and creating a macro variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962567#M375163</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
picture fmt
low-high='%0Y-%0m-%0d %0H:%0M:%0s'(datatype=datetime);
run;


data have;
    datetime='20MAR2025:00:00:00.000000'dt;
	format datetime fmt.;
    call symputx('datetime',put(datetime,fmt.));
run;
%put &amp;amp;=datetime.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Mar 2025 02:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Formatting-date-variable-and-creating-a-macro-variable/m-p/962567#M375163</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-03-25T02:12:27Z</dc:date>
    </item>
  </channel>
</rss>

