<?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: Passing a Macro Variable value into a data step in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791487#M253530</link>
    <description>&lt;P&gt;In general you probably do not want the quote characters in the macro variable. But it depends how you are using the macro variable.&amp;nbsp; Macro expressions will resolve inside of double quotes so just use those.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   sas_variable = "&amp;amp;macro_variable":
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really need to add single quotes, perhaps because you are using the value in pass through SQL, then use %BQUOTE().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
connect using mylib ;
create table want as select * from connection to mylib
(select * from sql_table where sql_variable = %bquote('&amp;amp;macro_variable') 
)
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 21 Jan 2022 16:29:19 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-01-21T16:29:19Z</dc:date>
    <item>
      <title>Passing a Macro Variable value into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791442#M253501</link>
      <description>&lt;P&gt;What I\m trying to do seems simple but I've been trying to figure it out for the last hour and still nothing.&amp;nbsp; I have a variable called NCCT_Date.&amp;nbsp; What ever I pass to this variable I would like to populate every row of an already create dataset and call the column the data is getting passed to Rpt_Date.&amp;nbsp; The code I'm using is shown below.&amp;nbsp; When I run the code the Rpt_Date column is created but the 'Fall2021' data is not getting passed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let NCCT_Date = Fall2021;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data work.sampletbl;&lt;/P&gt;&lt;P&gt;set work.sampletbl;&lt;/P&gt;&lt;P&gt;Rpt_Date = &amp;amp;NCCT_Date;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 14:57:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791442#M253501</guid>
      <dc:creator>Jabari</dc:creator>
      <dc:date>2022-01-21T14:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a Macro Variable value into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791447#M253506</link>
      <description>&lt;P&gt;Why use a macro variable when you don't need to?&lt;BR /&gt;Assuming Rpt_Date is a character variable then you need to quote the macro variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.sampletbl;
	set work.sampletbl;
	Rpt_Date = &amp;amp;NCCT_Date;
	Rpt_Date1 = "Fall2021" ;
	Rpt_Date2 = "&amp;amp;NCCT_Date";
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Jan 2022 15:04:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791447#M253506</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2022-01-21T15:04:02Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a Macro Variable value into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791452#M253508</link>
      <description>&lt;P&gt;Macro variables perform text substitution when the code is executed. The value of the macro variable replaces the actual macro variable in your code upon execution. And the result MUST be valid working SAS code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SO your line of code is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Rpt_Date = &amp;amp;NCCT_Date;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and when you execute the code, it becomes&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; Rpt_Date = Fall2021;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So let me ask you, is this valid SAS code? Does it work, does it do what you want? If there is a variable in your SAS dataset named Fall2021, this works. But it doesn't assign the value of a text string Fall2021 to Rpt_Date, because you haven't created working SAS code when the macro variable value is substituted into your code. Do you see why this won't work to do what you want?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, you should first always create working SAS code without macros and without macro variables. It appears you didn't do that. This is always a mandatory first step ... if you don't have working SAS code, then code with macro variables will never work either.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 15:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791452#M253508</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-01-21T15:18:36Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a Macro Variable value into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791464#M253511</link>
      <description>&lt;P&gt;I tried adding quotes to the macro so it could pass as 'Fall2021'.&amp;nbsp; When I check this in the log it is resolving as expected but still will not pass correctly to the data step.&amp;nbsp; I guess I'm asking what code do I need to write to pass 'Fall2021' correctly.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;NCCT_Date2 = %quote(%')&amp;amp;NCCT_Date%quote(%') ;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;NCCT_Date2;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 16:24:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791464#M253511</guid>
      <dc:creator>Jabari</dc:creator>
      <dc:date>2022-01-21T16:24:08Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a Macro Variable value into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791474#M253518</link>
      <description>&lt;P&gt;Why use a macro variable when you don't need to?&lt;BR /&gt;Assuming Rpt_Date is a character variable then you need to quote the macro variable&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data work.sampletbl;
	set work.sampletbl;
	Rpt_Date = &amp;amp;NCCT_Date;
	Rpt_Date1 = "Fall2021" ;
	Rpt_Date2 = "&amp;amp;NCCT_Date";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You need to understand how macro variables resolve. Here's some good reading:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p0b1hb28lkv9ujn1dfjj0xaddymb.htm" target="_self"&gt;Macro Variables Defined by Users&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/n0700fspmubii5n1vecutttwz93n.htm" target="_self"&gt;Using Macro Variables&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here's the example with some comments added, but I recommend you read the documentation&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;512  options symbolgen mprint ;
513  %let NCCT_Date = Fall2021;
514
515  data _null_ ;
516      /* Resolves to
517      Rpt_Date = Fall2021;
518      */
519      Rpt_Date = &amp;amp;NCCT_Date;
SYMBOLGEN:  Macro variable NCCT_DATE resolves to Fall2021
520      /* No macro resolution */
521      Rpt_Date1 = "Fall2021" ;
522      /* Resolves to
523      Rpt_Date2 = "Fall2021" ;
524      */
525      Rpt_Date2 = "&amp;amp;NCCT_Date";
SYMBOLGEN:  Macro variable NCCT_DATE resolves to Fall2021
526      put rpt_date= rpt_date1= rpt_date2= ;
527  run;

NOTE: Variable Fall2021 is uninitialized.
Rpt_Date=. Rpt_Date1=Fall2021 Rpt_Date2=Fall2021
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Note: Edited reply to create a more complete solution&lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Jun 2025 12:49:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791474#M253518</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2025-06-13T12:49:21Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a Macro Variable value into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791477#M253521</link>
      <description>&lt;P&gt;I need the macro because the "Fall2021" is used in several other places in the code.&amp;nbsp; But putting quotes around the NCCT_Date works.&amp;nbsp; I didnt think you could do that .&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 16:11:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791477#M253521</guid>
      <dc:creator>Jabari</dc:creator>
      <dc:date>2022-01-21T16:11:35Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a Macro Variable value into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791478#M253522</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/365837"&gt;@Jabari&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;LI-SPOILER&gt;I tried adding quotes to the macro so it could pass as 'Fall2021'.&amp;nbsp; When I check this in the log it is resolving as expected but still will not pass correctly to the data step.&amp;nbsp; I guess I'm asking what code do I need to write to pass 'Fall2021' correctly.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;NCCT_Date2 = %quote(%')&amp;amp;NCCT_Date%quote(%') ;&lt;BR /&gt;&lt;BR /&gt;%put &amp;amp;NCCT_Date2;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI-SPOILER&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you are using a macro function like %quote in a data step expect unexpected results. First there is a perfectly good non-macro function Quote (not needed).&lt;/P&gt;
&lt;P&gt;Second if you use macro variables frequently you will find that attaching the quotes to the macro variable will quite often be more complex to debug than using the basic "&amp;amp;macrovariable." when trying to use it in data step, procedure or other statements.&lt;/P&gt;
&lt;P&gt;Third you should show entire code of things that "don't work" and likely it should be from the LOG with all the notes and messages. For example your code above does not include a %let and does not appear in a data step so generates errors when attempting the assignment to NCCT_Date2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Jan 2022 16:12:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791478#M253522</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-01-21T16:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: Passing a Macro Variable value into a data step</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791487#M253530</link>
      <description>&lt;P&gt;In general you probably do not want the quote characters in the macro variable. But it depends how you are using the macro variable.&amp;nbsp; Macro expressions will resolve inside of double quotes so just use those.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   set have;
   sas_variable = "&amp;amp;macro_variable":
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you really need to add single quotes, perhaps because you are using the value in pass through SQL, then use %BQUOTE().&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
connect using mylib ;
create table want as select * from connection to mylib
(select * from sql_table where sql_variable = %bquote('&amp;amp;macro_variable') 
)
;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 21 Jan 2022 16:29:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Passing-a-Macro-Variable-value-into-a-data-step/m-p/791487#M253530</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-01-21T16:29:19Z</dc:date>
    </item>
  </channel>
</rss>

