<?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 variable in SQL in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54291#M11490</link>
    <description>Hi:&lt;BR /&gt;
  That is very strange. "&amp;amp;macvar"d -should- work in a WHERE clause. See example below. As you can see below, you need to use quotes where appropriate. If I had these 2 where clauses:&lt;BR /&gt;
[pre]&lt;BR /&gt;
where bday = -3334;&lt;BR /&gt;
OR&lt;BR /&gt;
where bday = '15NOV1950'd;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                  &lt;BR /&gt;
and I was going to turn them into code that used a macro reference, the FIRST thing I'd have to do was realize that I could not use a macro  variable in single quotes, so that second WHERE would change to:&lt;BR /&gt;
   &lt;BR /&gt;
[pre]&lt;BR /&gt;
where bday = "15NOV1950"d;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
  &lt;BR /&gt;
NOW, I'm ready to create and use my macro variables:&lt;BR /&gt;
&lt;BR /&gt;
%let usenum = -3334;&lt;BR /&gt;
%let useconstant = 15NOV1950;&lt;BR /&gt;
     &lt;BR /&gt;
[pre]&lt;BR /&gt;
working code &lt;BR /&gt;
          where bday = -3334;&lt;BR /&gt;
becomes &lt;BR /&gt;
          where bday = &amp;amp;usenum;&lt;BR /&gt;
AND&lt;BR /&gt;
working code&lt;BR /&gt;
          where bday = '15NOV1950'd;&lt;BR /&gt;
becomes&lt;BR /&gt;
          where bday = "&amp;amp;useconstant"d;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
             &lt;BR /&gt;
Note that the correct WORKING syntax determines the use of quotes or not. If I am going to use the internal date number (-3334), then the where clause does not need quotes. However, if I plan to use the string for bday (15Nov1950), then I have to put double quotes and a d (for date constant) in my where clause, so that the where clause successfully transforms the string into a date value. The quotes and the d belong to the -use- of the string for date in the WHERE clause. Nothing happens in the %LET statement if you use '15Nov1950'd as the value for a macro variable -- that is just another string, but with quotes and a d added to the string. &lt;BR /&gt;
                     &lt;BR /&gt;
I disagree with those who recommend putting quotes in the macro variable: &lt;BR /&gt;
%let macvar = '15Nov1950'd; -- generally, I find it is not a good practice to try to "pre-quote" macro variables -- because, it limits the ways I can use the macro variable if the quotes are already embedded in the macro variable value.&lt;BR /&gt;
 &lt;BR /&gt;
If you still find that "&amp;amp;macvar"d does not work in your where clause, perhaps you should open a track with Tech Support so they can look at your EXACT code and your data to see if they can help you figure out what's wrong.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
&lt;BR /&gt;
** the code to test;&lt;BR /&gt;
ods listing;&lt;BR /&gt;
                   &lt;BR /&gt;
data bday;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input name $ bday : mmddyy10.;&lt;BR /&gt;
  format bday date9.;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
drew   11/15/1950&lt;BR /&gt;
kermit 05/09/1955&lt;BR /&gt;
george 11/19/1966&lt;BR /&gt;
sarah  11/29/1984&lt;BR /&gt;
jim    05/24/1936&lt;BR /&gt;
lou    11/15/1950&lt;BR /&gt;
sue    11/15/1950&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
               &lt;BR /&gt;
%let usenum = -3334;&lt;BR /&gt;
%let useconstant = 15Nov1950;&lt;BR /&gt;
              &lt;BR /&gt;
title '1) use internal number for date';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where bday = - 3334;&lt;BR /&gt;
quit;&lt;BR /&gt;
                      &lt;BR /&gt;
title '2) use date constant for date';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where bday = "15nov1950"d;&lt;BR /&gt;
quit;&lt;BR /&gt;
               &lt;BR /&gt;
title '3) use macro variable with double quotes for date';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where bday = "&amp;amp;useconstant"d;&lt;BR /&gt;
quit;&lt;BR /&gt;
                     &lt;BR /&gt;
title '4) use macro variable with date number';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where bday = &amp;amp;usenum;&lt;BR /&gt;
quit;&lt;BR /&gt;
                 &lt;BR /&gt;
title '5) Use Function with internal number';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where month(bday) = month(&amp;amp;usenum);&lt;BR /&gt;
quit;&lt;BR /&gt;
                                             &lt;BR /&gt;
title '6) use function with date constant and double quotes';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where month(bday) = month("&amp;amp;useconstant"d);&lt;BR /&gt;
quit;</description>
    <pubDate>Tue, 14 Jul 2009 15:08:40 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2009-07-14T15:08:40Z</dc:date>
    <item>
      <title>Macro variable in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54287#M11486</link>
      <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
how would I use a macro variable, which represents an integer value of a date (e.g. 17889) in a comparison with a DATE9. value?&lt;BR /&gt;
&lt;BR /&gt;
e.g.&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL;&lt;BR /&gt;
&lt;BR /&gt;
CREATE TABLE work.test AS&lt;BR /&gt;
SELECT a, b &lt;BR /&gt;
FROM c	&lt;BR /&gt;
WHERE&lt;BR /&gt;
d &amp;lt; &amp;amp;_macrovariable;&lt;BR /&gt;
&lt;BR /&gt;
QUIT;&lt;BR /&gt;
&lt;BR /&gt;
Thanks a lot,&lt;BR /&gt;
Thomas

Message was edited by: ThomasH</description>
      <pubDate>Tue, 14 Jul 2009 08:55:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54287#M11486</guid>
      <dc:creator>ThomasH</dc:creator>
      <dc:date>2009-07-14T08:55:12Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54288#M11487</link>
      <description>Hello Thomas.&lt;BR /&gt;
&lt;BR /&gt;
17889 is exactly the same as '23DEC2008'd&lt;BR /&gt;
&lt;BR /&gt;
Only difference is that the latest has a DATE9. format applied to it, but really its just a number.&lt;BR /&gt;
Now, if you wish to assign a date value with the DATE9. notation you should enclose your date between quotes and the use the d suffix. In this form, SAS will read the formatted value and convert it to a numeric value, for calculations.&lt;BR /&gt;
&lt;BR /&gt;
If you run the code above:&lt;BR /&gt;
&lt;BR /&gt;
data _null_;&lt;BR /&gt;
X=17889;&lt;BR /&gt;
Y='23DEC2008'd;&lt;BR /&gt;
put X date9.;&lt;BR /&gt;
put Y 5.;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
The result will be the following:&lt;BR /&gt;
&lt;BR /&gt;
23DEC2008&lt;BR /&gt;
17889&lt;BR /&gt;
&lt;BR /&gt;
Check the online SAS documentation for dealing with date values:&lt;BR /&gt;
&lt;A href="http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001304321.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/basess/58133/HTML/default/a001304321.htm&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;.</description>
      <pubDate>Tue, 14 Jul 2009 09:14:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54288#M11487</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-07-14T09:14:10Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54289#M11488</link>
      <description>Hi Daniel,&lt;BR /&gt;
&lt;BR /&gt;
thanks for your quick reply.&lt;BR /&gt;
&lt;BR /&gt;
Of course I tried this notation already but it doesnt work with "&amp;amp;_macrovariable"d either.&lt;BR /&gt;
&lt;BR /&gt;
Any other ideas? I need to use it in a PROC SQL and not in a data step.&lt;BR /&gt;
&lt;BR /&gt;
Thanks,&lt;BR /&gt;
Thomas

Message was edited by: ThomasH</description>
      <pubDate>Tue, 14 Jul 2009 09:18:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54289#M11488</guid>
      <dc:creator>ThomasH</dc:creator>
      <dc:date>2009-07-14T09:18:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54290#M11489</link>
      <description>OK then...&lt;BR /&gt;
&lt;BR /&gt;
%let _macrovariable=23DEC2008;&lt;BR /&gt;
...&lt;BR /&gt;
WHERE d lt "&amp;amp;_macrovariable"d;&lt;BR /&gt;
&lt;BR /&gt;
or&lt;BR /&gt;
&lt;BR /&gt;
%let _macrovariable='23DEC2009'd;&lt;BR /&gt;
...&lt;BR /&gt;
WHERE d lt &amp;amp;_macrovariable;&lt;BR /&gt;
&lt;BR /&gt;
or&lt;BR /&gt;
&lt;BR /&gt;
%let _macrovariabe=17889;&lt;BR /&gt;
...&lt;BR /&gt;
WHERE d lt &amp;amp;_macrovariable;&lt;BR /&gt;
&lt;BR /&gt;
Are all equivalent and should work.&lt;BR /&gt;
&lt;BR /&gt;
Cheers from Portugal.&lt;BR /&gt;
&lt;BR /&gt;
Daniel Santos @ &lt;A href="http://www.cgd.pt" target="_blank"&gt;www.cgd.pt&lt;/A&gt;.</description>
      <pubDate>Tue, 14 Jul 2009 09:23:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54290#M11489</guid>
      <dc:creator>DanielSantos</dc:creator>
      <dc:date>2009-07-14T09:23:37Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54291#M11490</link>
      <description>Hi:&lt;BR /&gt;
  That is very strange. "&amp;amp;macvar"d -should- work in a WHERE clause. See example below. As you can see below, you need to use quotes where appropriate. If I had these 2 where clauses:&lt;BR /&gt;
[pre]&lt;BR /&gt;
where bday = -3334;&lt;BR /&gt;
OR&lt;BR /&gt;
where bday = '15NOV1950'd;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
                  &lt;BR /&gt;
and I was going to turn them into code that used a macro reference, the FIRST thing I'd have to do was realize that I could not use a macro  variable in single quotes, so that second WHERE would change to:&lt;BR /&gt;
   &lt;BR /&gt;
[pre]&lt;BR /&gt;
where bday = "15NOV1950"d;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
  &lt;BR /&gt;
NOW, I'm ready to create and use my macro variables:&lt;BR /&gt;
&lt;BR /&gt;
%let usenum = -3334;&lt;BR /&gt;
%let useconstant = 15NOV1950;&lt;BR /&gt;
     &lt;BR /&gt;
[pre]&lt;BR /&gt;
working code &lt;BR /&gt;
          where bday = -3334;&lt;BR /&gt;
becomes &lt;BR /&gt;
          where bday = &amp;amp;usenum;&lt;BR /&gt;
AND&lt;BR /&gt;
working code&lt;BR /&gt;
          where bday = '15NOV1950'd;&lt;BR /&gt;
becomes&lt;BR /&gt;
          where bday = "&amp;amp;useconstant"d;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
             &lt;BR /&gt;
Note that the correct WORKING syntax determines the use of quotes or not. If I am going to use the internal date number (-3334), then the where clause does not need quotes. However, if I plan to use the string for bday (15Nov1950), then I have to put double quotes and a d (for date constant) in my where clause, so that the where clause successfully transforms the string into a date value. The quotes and the d belong to the -use- of the string for date in the WHERE clause. Nothing happens in the %LET statement if you use '15Nov1950'd as the value for a macro variable -- that is just another string, but with quotes and a d added to the string. &lt;BR /&gt;
                     &lt;BR /&gt;
I disagree with those who recommend putting quotes in the macro variable: &lt;BR /&gt;
%let macvar = '15Nov1950'd; -- generally, I find it is not a good practice to try to "pre-quote" macro variables -- because, it limits the ways I can use the macro variable if the quotes are already embedded in the macro variable value.&lt;BR /&gt;
 &lt;BR /&gt;
If you still find that "&amp;amp;macvar"d does not work in your where clause, perhaps you should open a track with Tech Support so they can look at your EXACT code and your data to see if they can help you figure out what's wrong.&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
&lt;BR /&gt;
** the code to test;&lt;BR /&gt;
ods listing;&lt;BR /&gt;
                   &lt;BR /&gt;
data bday;&lt;BR /&gt;
  infile datalines;&lt;BR /&gt;
  input name $ bday : mmddyy10.;&lt;BR /&gt;
  format bday date9.;&lt;BR /&gt;
return;&lt;BR /&gt;
datalines;&lt;BR /&gt;
drew   11/15/1950&lt;BR /&gt;
kermit 05/09/1955&lt;BR /&gt;
george 11/19/1966&lt;BR /&gt;
sarah  11/29/1984&lt;BR /&gt;
jim    05/24/1936&lt;BR /&gt;
lou    11/15/1950&lt;BR /&gt;
sue    11/15/1950&lt;BR /&gt;
;&lt;BR /&gt;
run;&lt;BR /&gt;
               &lt;BR /&gt;
%let usenum = -3334;&lt;BR /&gt;
%let useconstant = 15Nov1950;&lt;BR /&gt;
              &lt;BR /&gt;
title '1) use internal number for date';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where bday = - 3334;&lt;BR /&gt;
quit;&lt;BR /&gt;
                      &lt;BR /&gt;
title '2) use date constant for date';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where bday = "15nov1950"d;&lt;BR /&gt;
quit;&lt;BR /&gt;
               &lt;BR /&gt;
title '3) use macro variable with double quotes for date';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where bday = "&amp;amp;useconstant"d;&lt;BR /&gt;
quit;&lt;BR /&gt;
                     &lt;BR /&gt;
title '4) use macro variable with date number';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where bday = &amp;amp;usenum;&lt;BR /&gt;
quit;&lt;BR /&gt;
                 &lt;BR /&gt;
title '5) Use Function with internal number';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where month(bday) = month(&amp;amp;usenum);&lt;BR /&gt;
quit;&lt;BR /&gt;
                                             &lt;BR /&gt;
title '6) use function with date constant and double quotes';&lt;BR /&gt;
proc sql;&lt;BR /&gt;
   select * &lt;BR /&gt;
   from work.bday&lt;BR /&gt;
   where month(bday) = month("&amp;amp;useconstant"d);&lt;BR /&gt;
quit;</description>
      <pubDate>Tue, 14 Jul 2009 15:08:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54291#M11490</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-07-14T15:08:40Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable in SQL</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54292#M11491</link>
      <description>Well, guess I was thinking too complicated - that worked, thanks a lot! &lt;BR /&gt;
&lt;BR /&gt;
Thomas

Message was edited by: ThomasH</description>
      <pubDate>Tue, 14 Jul 2009 16:37:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-variable-in-SQL/m-p/54292#M11491</guid>
      <dc:creator>ThomasH</dc:creator>
      <dc:date>2009-07-14T16:37:51Z</dc:date>
    </item>
  </channel>
</rss>

