<?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: How to refer a variable in a where statement and in the name of the table using %let in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726853#M28158</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;has already given a sufficient and satisfactory answer I guess, but here's an alternative (to working with %sysfunc()).&lt;/P&gt;
&lt;P&gt;I think this is what you're after:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
 initial_date='01Feb2021'd;         put initial_date date9.; 
 final_date='28Feb2021'd;           put final_date date9.; 
 initial_year=year(initial_date);   put initial_year; 
 final_year  =year(final_date);     put final_year; 
 initial_month=month(initial_date); put initial_month; 
 final_month  =month(final_date);   put final_month;
 call symput('initial_date' ,"'"!!put(initial_date,date9.)!!"'d");
 call symput('final_date'   ,"'"!!put(final_date,date9.)!!"'d"); 
 call symput('initial_year' ,put(initial_year,4.)); 
 call symput('final_year'   ,put(final_year,4.)); 
 call symput('initial_month',strip(put(initial_month,2.))); 
 call symput('final_month'  ,strip(put(initial_month,2.))); 
run;

%PUT *****&amp;amp;=initial_date*****;
%PUT *****&amp;amp;=final_date*****;
%PUT *****&amp;amp;=initial_year*****;
%PUT *****&amp;amp;=final_year*****;
%PUT *****&amp;amp;=initial_month*****;
%PUT *****&amp;amp;=final_month*****;

data work.vendas_&amp;amp;initial_year._&amp;amp;initial_month._&amp;amp;final_year._&amp;amp;final_month.;
 set dataw.f_Sales;
where (     (data&amp;gt;=&amp;amp;initial_date. and data&amp;lt;&amp;amp;final_date.) 
        AND (TIPOLIN = 'V') 
        AND (substr(id,1,1) in ('2','3','4','5','6','7','8','9') ) );
run;
/* end of program */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Mar 2021 18:18:56 GMT</pubDate>
    <dc:creator>sbxkoenk</dc:creator>
    <dc:date>2021-03-16T18:18:56Z</dc:date>
    <item>
      <title>How to refer a variable in a where statement and in the name of the table using %let</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726835#M28153</link>
      <description>&lt;P&gt;Dear experts, I want to use some %let&amp;nbsp;variables in the following program but it is not working.&lt;/P&gt;&lt;P&gt;The where statement&amp;nbsp;seems to be ok but the name of the table that I want to create in not.&lt;/P&gt;&lt;P&gt;And If I create a variable like X=&amp;amp;Initial_Year;&amp;nbsp;I get a column with the result spread.&lt;/P&gt;&lt;P&gt;Thank You for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let initial_date='01Feb2021';&lt;BR /&gt;%let final_date='28Feb2021';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let Initial_Year=year(&amp;amp;data_inicial.d);&lt;BR /&gt;%let final_Year=year(&amp;amp;data_final.d);&lt;BR /&gt;%let initial_month=month(&amp;amp;data_inicial.d);&lt;BR /&gt;%let final_month=month(&amp;amp;data_final.d);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data vendas_&amp;amp;Initial_Year._&amp;amp;&amp;amp;initial_month._&amp;amp;&amp;amp;final_Year._&amp;amp;&amp;amp; final_month.;&lt;BR /&gt;set dataw.f_Sales;&lt;/P&gt;&lt;P&gt;where ((data&amp;gt;=&amp;amp;dinitial_date.d and data&amp;lt;&amp;amp;inal_date.d) AND (TIPOLIN = 'V') AND (substr(id,1,1) in ('2','3','4','5','6','7','8','9')));&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 17:30:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726835#M28153</guid>
      <dc:creator>PauloC</dc:creator>
      <dc:date>2021-03-16T17:30:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to refer a variable in a where statement and in the name of the table using %let</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726842#M28154</link>
      <description>&lt;P&gt;When your program throws as many errors as I think it does you really need to include the LOG.&lt;/P&gt;
&lt;P&gt;Please double check you code and read the LOG.&lt;/P&gt;
&lt;P&gt;I get this from running your %let statements:&lt;/P&gt;
&lt;PRE&gt;20   %let initial_date='01Feb2021';
21   %let final_date='28Feb2021';
22
23   %let Initial_Year=year(&amp;amp;data_inicial.d);
WARNING: Apparent symbolic reference DATA_INICIAL not resolved.
24   %let final_Year=year(&amp;amp;data_final.d);
WARNING: Apparent symbolic reference DATA_FINAL not resolved.
25   %let initial_month=month(&amp;amp;data_inicial.d);
WARNING: Apparent symbolic reference DATA_INICIAL not resolved.
26   %let final_month=month(&amp;amp;data_final.d);
WARNING: Apparent symbolic reference DATA_FINAL not resolved.


&lt;/PRE&gt;
&lt;P&gt;So if that was you code the &lt;STRONG&gt;first&lt;/STRONG&gt; problem is spelling variable names correctly. You show repeated use of &amp;amp;DATA_INICIAL but the variable you created with %let is INITIAL_DATE, and you use &amp;amp;data_final but created FINAL_DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, if you want to use data step functions like YEAR, MONTH or anything else the code has to be wrapped in a %sysfunc(&amp;nbsp;&amp;nbsp; ) to tell SAS that you want the macro processor to use the function and not just use the TEXT.&lt;/P&gt;
&lt;P&gt;Between your first and second problems your log should have show a bunch of undefined macro variable warnings.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Third, it is a good idea to check your macro variable contents, especially when trying something new, by using %PUT to check on the results. Try this and see if that helps with the table names.&lt;/P&gt;
&lt;PRE&gt;%let initial_date='01Feb2021';
%let final_date='28Feb2021';

%let Initial_Year =%sysfunc(year(&amp;amp;initial_date.d));
%let final_Year   =%sysfunc(year(&amp;amp;final_date.d));
%let initial_month=%sysfunc(month(&amp;amp;initial_date.d));
%let final_month  =%sysfunc(month(&amp;amp;final_date.d));

%put Initial_Yea: &amp;amp;Initial_Year Final_year: &amp;amp;final_year ;
%put Initial_month: &amp;amp;initial_month  Final_month: &amp;amp;final_month;&lt;/PRE&gt;
&lt;P&gt;Experience often leads SAS programs to consider placing quotes as part of macro variables often sub-optimal coding.&lt;/P&gt;
&lt;P&gt;Turn text in to dates, if needed by placing unquoted values in "&amp;amp;macrovar"d instead, and may even want actual date values instead of the text.&lt;/P&gt;
&lt;P&gt;The text version could be used in other statements if you don't have the quotes like a title statement:&lt;/P&gt;
&lt;P&gt;Title "Report for data from &amp;amp;initial_date to &amp;amp;final_date";&lt;/P&gt;
&lt;P&gt;If those macro variables have quotes though then the syntax may either not run or just look funny.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 17:44:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726842#M28154</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-16T17:44:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to refer a variable in a where statement and in the name of the table using %let</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726853#M28158</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;has already given a sufficient and satisfactory answer I guess, but here's an alternative (to working with %sysfunc()).&lt;/P&gt;
&lt;P&gt;I think this is what you're after:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _NULL_;
 initial_date='01Feb2021'd;         put initial_date date9.; 
 final_date='28Feb2021'd;           put final_date date9.; 
 initial_year=year(initial_date);   put initial_year; 
 final_year  =year(final_date);     put final_year; 
 initial_month=month(initial_date); put initial_month; 
 final_month  =month(final_date);   put final_month;
 call symput('initial_date' ,"'"!!put(initial_date,date9.)!!"'d");
 call symput('final_date'   ,"'"!!put(final_date,date9.)!!"'d"); 
 call symput('initial_year' ,put(initial_year,4.)); 
 call symput('final_year'   ,put(final_year,4.)); 
 call symput('initial_month',strip(put(initial_month,2.))); 
 call symput('final_month'  ,strip(put(initial_month,2.))); 
run;

%PUT *****&amp;amp;=initial_date*****;
%PUT *****&amp;amp;=final_date*****;
%PUT *****&amp;amp;=initial_year*****;
%PUT *****&amp;amp;=final_year*****;
%PUT *****&amp;amp;=initial_month*****;
%PUT *****&amp;amp;=final_month*****;

data work.vendas_&amp;amp;initial_year._&amp;amp;initial_month._&amp;amp;final_year._&amp;amp;final_month.;
 set dataw.f_Sales;
where (     (data&amp;gt;=&amp;amp;initial_date. and data&amp;lt;&amp;amp;final_date.) 
        AND (TIPOLIN = 'V') 
        AND (substr(id,1,1) in ('2','3','4','5','6','7','8','9') ) );
run;
/* end of program */
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Cheers,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Mar 2021 18:18:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726853#M28158</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-03-16T18:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to refer a variable in a where statement and in the name of the table using %let</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726884#M28162</link>
      <description>Thanks for the help and sorry for my mistakes in the name of the variables.</description>
      <pubDate>Tue, 16 Mar 2021 19:40:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726884#M28162</guid>
      <dc:creator>PauloC</dc:creator>
      <dc:date>2021-03-16T19:40:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to refer a variable in a where statement and in the name of the table using %let</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726885#M28163</link>
      <description>Thanks for the alternative, so I can learn more ...</description>
      <pubDate>Tue, 16 Mar 2021 19:43:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-refer-a-variable-in-a-where-statement-and-in-the-name-of/m-p/726885#M28163</guid>
      <dc:creator>PauloC</dc:creator>
      <dc:date>2021-03-16T19:43:10Z</dc:date>
    </item>
  </channel>
</rss>

