<?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 Help with macro variables in ODS and Base Reporting</title>
    <link>https://communities.sas.com/t5/ODS-and-Base-Reporting/Help-with-macro-variables/m-p/3465#M1491</link>
    <description>I’m trying to set a macro date variable and use that variable within a sql statement but I can’t seem to get the syntax right.  &lt;BR /&gt;
&lt;BR /&gt;
Example:&lt;BR /&gt;
&lt;BR /&gt;
%let end_date   =          %sysfunc(today(),mmddyy10.);&lt;BR /&gt;
%let beg_date   =          &amp;amp;end_date – 60;&lt;BR /&gt;
&lt;BR /&gt;
Select column1&lt;BR /&gt;
From table1&lt;BR /&gt;
Where datecolumn between &amp;amp;beg_date and &amp;amp;end_date&lt;BR /&gt;
&lt;BR /&gt;
The end_date variable is set to the current system date and that works fine but I can’t seem to set the second variable to be 60 days prior to the current date.  I’ve tried many different variations and I can’t figure it out.  &lt;BR /&gt;
&lt;BR /&gt;
I can do this within a data step but It doesn’t work with a macro variable.</description>
    <pubDate>Mon, 18 Jun 2007 15:06:27 GMT</pubDate>
    <dc:creator>rechavarri</dc:creator>
    <dc:date>2007-06-18T15:06:27Z</dc:date>
    <item>
      <title>Help with macro variables</title>
      <link>https://communities.sas.com/t5/ODS-and-Base-Reporting/Help-with-macro-variables/m-p/3465#M1491</link>
      <description>I’m trying to set a macro date variable and use that variable within a sql statement but I can’t seem to get the syntax right.  &lt;BR /&gt;
&lt;BR /&gt;
Example:&lt;BR /&gt;
&lt;BR /&gt;
%let end_date   =          %sysfunc(today(),mmddyy10.);&lt;BR /&gt;
%let beg_date   =          &amp;amp;end_date – 60;&lt;BR /&gt;
&lt;BR /&gt;
Select column1&lt;BR /&gt;
From table1&lt;BR /&gt;
Where datecolumn between &amp;amp;beg_date and &amp;amp;end_date&lt;BR /&gt;
&lt;BR /&gt;
The end_date variable is set to the current system date and that works fine but I can’t seem to set the second variable to be 60 days prior to the current date.  I’ve tried many different variations and I can’t figure it out.  &lt;BR /&gt;
&lt;BR /&gt;
I can do this within a data step but It doesn’t work with a macro variable.</description>
      <pubDate>Mon, 18 Jun 2007 15:06:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/ODS-and-Base-Reporting/Help-with-macro-variables/m-p/3465#M1491</guid>
      <dc:creator>rechavarri</dc:creator>
      <dc:date>2007-06-18T15:06:27Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro variables</title>
      <link>https://communities.sas.com/t5/ODS-and-Base-Reporting/Help-with-macro-variables/m-p/3466#M1492</link>
      <description>Hi:&lt;BR /&gt;
  This is not an ODS question. &lt;BR /&gt;
     &lt;BR /&gt;
  In general, when working with SAS Macro variables, it is a good idea to have a working SAS program BEFORE you convert the program to use SAS macro variables.&lt;BR /&gt;
 &lt;BR /&gt;
  For example, given this data file: &lt;BR /&gt;
[pre]&lt;BR /&gt;
data table1;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input datecolumn : mmddyy10. column1;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
03/01/2007 1111&lt;BR /&gt;
04/15/2007 2222&lt;BR /&gt;
05/16/2007 3333&lt;BR /&gt;
06/18/2007 4444&lt;BR /&gt;
07/04/2007 5555&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
   &lt;BR /&gt;
ods listing;&lt;BR /&gt;
proc print data=table1;&lt;BR /&gt;
title 'note how dates are internally stored as numbers';&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
No matter HOW the date value is read into SAS, note how the datecolumn variable is represented in PROC PRINT without any formatting -- date values are just the number of days from Jan 1, 1960 (the zero date in SAS):&lt;BR /&gt;
[pre]&lt;BR /&gt;
note how dates are internally stored as numbers&lt;BR /&gt;
&lt;BR /&gt;
Obs    datecolumn    column1&lt;BR /&gt;
&lt;BR /&gt;
 1        17226        1111&lt;BR /&gt;
 2        17271        2222&lt;BR /&gt;
 3        17302        3333&lt;BR /&gt;
 4        17335        4444&lt;BR /&gt;
 5        17351        5555&lt;BR /&gt;
[/pre]&lt;BR /&gt;
So, today's date is 17335 and 60 days before today is 17275 (17335 - 60). If I had used a format statement in the PROC PRINT, the date variable would STILL be a number internally, SAS just would have displayed that number in a readable date format.&lt;BR /&gt;
 &lt;BR /&gt;
Once you know that the date is stored as a number internally, then you can build the correct SQL WHERE clause to do the selection. Consider these 4 SQL queries -- each one is attempting to run against TABLE1 -- using a different form for the date value. Only 2 of these queries will work:&lt;BR /&gt;
[pre]&lt;BR /&gt;
** this query will work;&lt;BR /&gt;
title '1: Use a number that represents the date';&lt;BR /&gt;
 &lt;BR /&gt;
proc sql;&lt;BR /&gt;
Select datecolumn format=mmddyy10. , column1&lt;BR /&gt;
From table1&lt;BR /&gt;
Where datecolumn between 17275 and 17335;&lt;BR /&gt;
quit;&lt;BR /&gt;
 &lt;BR /&gt;
** this query will work;&lt;BR /&gt;
title '2: Use a date constant in the WHERE clause';&lt;BR /&gt;
 &lt;BR /&gt;
proc sql;&lt;BR /&gt;
Select datecolumn format=mmddyy10. , column1&lt;BR /&gt;
From table1&lt;BR /&gt;
Where datecolumn between "19Apr2007"d and "18jun2007"d;&lt;BR /&gt;
quit;&lt;BR /&gt;
  &lt;BR /&gt;
** this query will not work because it is not the right;&lt;BR /&gt;
** form for a SAS date constant;&lt;BR /&gt;
  &lt;BR /&gt;
title '3: Try to use "mm/dd/yyyy"d as a date constant in the WHERE clause';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
Select datecolumn format=mmddyy10. , column1&lt;BR /&gt;
From table1&lt;BR /&gt;
Where datecolumn between "04/19/2007"d and "06/18/2007"d;&lt;BR /&gt;
quit;&lt;BR /&gt;
  &lt;BR /&gt;
** this query will not work because the internal value;&lt;BR /&gt;
** for datecolumn is a NUMBER and the dates as shown below;&lt;BR /&gt;
** are invalid in the query;&lt;BR /&gt;
title '4: Try to use mmddyy10 form in the WHERE clause';&lt;BR /&gt;
  &lt;BR /&gt;
proc sql;&lt;BR /&gt;
Select datecolumn format=mmddyy10. , column1&lt;BR /&gt;
From table1&lt;BR /&gt;
Where datecolumn between 04/19/2007 and 06/18/2007;&lt;BR /&gt;
quit;&lt;BR /&gt;
  &lt;BR /&gt;
[/pre]&lt;BR /&gt;
 &lt;BR /&gt;
When you use macro variables, you are creating TEXT strings. So, when you are manipulating the date variables as macro variables, what you are doing here:&lt;B&gt;&lt;BR /&gt;
%let beg_date = &amp;amp;end_date – 60;&lt;/B&gt;&lt;BR /&gt;
is the equivalent of trying to do this: "06/18/2007" - 60 &lt;BR /&gt;
Even if 06 divided by 18 divided by 2007 would evaluate in the %LET statement, it would NOT be a date value anymore.&lt;BR /&gt;
 &lt;BR /&gt;
For more help with this code and macro variable reference issues, your best bet is to contact Tech Support.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia</description>
      <pubDate>Mon, 18 Jun 2007 18:17:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/ODS-and-Base-Reporting/Help-with-macro-variables/m-p/3466#M1492</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2007-06-18T18:17:24Z</dc:date>
    </item>
    <item>
      <title>Re: Help with macro variables</title>
      <link>https://communities.sas.com/t5/ODS-and-Base-Reporting/Help-with-macro-variables/m-p/3467#M1493</link>
      <description>If you evaluate arithmetic or logical expressions you have to use the macro functions %EVAL for integer and %SYSEVALF for floating-point arithmetic.&lt;BR /&gt;
&lt;BR /&gt;
In this case your example would look like&lt;BR /&gt;
&lt;BR /&gt;
%let end_date = %sysfunc(today(),5.);&lt;BR /&gt;
%let beg_date = %eval(&amp;amp;end_date - 60);&lt;BR /&gt;
&lt;BR /&gt;
If datecolumn is a SAS Date Variable your SQL should work.&lt;BR /&gt;
&lt;BR /&gt;
regards, Peter</description>
      <pubDate>Fri, 22 Jun 2007 09:11:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/ODS-and-Base-Reporting/Help-with-macro-variables/m-p/3467#M1493</guid>
      <dc:creator>PeterMehnert</dc:creator>
      <dc:date>2007-06-22T09:11:10Z</dc:date>
    </item>
  </channel>
</rss>

