<?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: I would like to use a sas variable in Proc SQL from statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957110#M373656</link>
    <description>&lt;P&gt;We're missing some information, like what is the value of the macro variable&amp;nbsp;&lt;SPAN&gt;&amp;amp;TodaysDate? And do you have a LIBNAME statement for MYFOLDER? Maybe you could provide more of the code before PROC SQL.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nevertheless, treating dates as strings is simply a difficult way to proceed. Better to treat them as dates. Something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let todaysdate = %sysfunc(today());
%let tabledate = %sysfunc(intnx(month,&amp;amp;todaysdate,-1,e));

PROC SQL;
CREATE TABLE WORK.RandomName AS SELECT DISTINCT ID
FROM MyFolder.Table%sysfunc(putn(&amp;amp;tabledate,yymmddn8.))_I
WHERE ID = '12345678' AND CategoryID NOT IN ('45678','123789');
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note: in the FROM statement, there are no spaces (your original code had spaces in there which won't work)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note: you said "&lt;SPAN&gt;yyyymmdd, where dd is the last day of the previous month." If this is something you have designed, make your life easier by using just yyyymm instead of yyyymmdd.&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 24 Jan 2025 13:55:03 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2025-01-24T13:55:03Z</dc:date>
    <item>
      <title>I would like to use a sas variable in Proc SQL from statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957107#M373653</link>
      <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to use a SAS variable in my proc SQL's from statement, in order to create a dynamic query to automate monthly data requests.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to retrieve a table from a server, from a specific folder. Lets call the folder "MyFolder".&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are monthly data table uploads to this folder, using a time stamp like yyyymmdd, where dd is the last day of the previous month.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like my code, to always get the right date, without any extra manual work, and retrieve the appropriate table.&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;%let TableDate = substr(put(intnx('month',intnx('month',&amp;amp;TodaysDate.,-1),0,'E'),yymmddn8.),3,6);&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;PROC SQL;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CREATE TABLE WORK.RandomName AS SELECT DISTINCT&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;ID&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;FROM MyFolder.Table &amp;amp;TableDate. _I&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;WHERE ID = '12345678' AND CategoryID NOT IN ('45678','123789');&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;QUIT;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;I checked, and the TableDate returns the value I would expect it to return, the issue is, that when I try to use it in the from statement, for some reason it does not seem to translate? (Returns the code inside the variable instead of the value I desire).&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;May I request Your kind help on this topic?&amp;nbsp;&lt;BR /&gt;I have been trying to resolve this issue for more time than I would like to admit, researched the forum, but could not find anything that would help me out.&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks in advance! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 24 Jan 2025 13:46:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957107#M373653</guid>
      <dc:creator>Jasuo</dc:creator>
      <dc:date>2025-01-24T13:46:10Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to use a sas variable in Proc SQL from statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957108#M373654</link>
      <description>Your macro variable contains the whole code you stored in it. When used in a DATA step, the function calls work, but the code will not work when used as part of a dataset name.&lt;BR /&gt;Wrap the function calls in %SYSFUNC so that they are executed during the macro variable creation.</description>
      <pubDate>Fri, 24 Jan 2025 13:37:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957108#M373654</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-01-24T13:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to use a sas variable in Proc SQL from statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957109#M373655</link>
      <description>&lt;P&gt;And since %SYSFUNC allows formatting of the result, your function call would look like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let TableDate = %sysfunc(intnx(month,%sysfunc(today()),-1,e),yymmn4.);

%put &amp;amp;=tabledate;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 24 Jan 2025 13:44:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957109#M373655</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2025-01-24T13:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to use a sas variable in Proc SQL from statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957110#M373656</link>
      <description>&lt;P&gt;We're missing some information, like what is the value of the macro variable&amp;nbsp;&lt;SPAN&gt;&amp;amp;TodaysDate? And do you have a LIBNAME statement for MYFOLDER? Maybe you could provide more of the code before PROC SQL.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nevertheless, treating dates as strings is simply a difficult way to proceed. Better to treat them as dates. Something like this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let todaysdate = %sysfunc(today());
%let tabledate = %sysfunc(intnx(month,&amp;amp;todaysdate,-1,e));

PROC SQL;
CREATE TABLE WORK.RandomName AS SELECT DISTINCT ID
FROM MyFolder.Table%sysfunc(putn(&amp;amp;tabledate,yymmddn8.))_I
WHERE ID = '12345678' AND CategoryID NOT IN ('45678','123789');
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note: in the FROM statement, there are no spaces (your original code had spaces in there which won't work)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please note: you said "&lt;SPAN&gt;yyyymmdd, where dd is the last day of the previous month." If this is something you have designed, make your life easier by using just yyyymm instead of yyyymmdd.&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 24 Jan 2025 13:55:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957110#M373656</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-01-24T13:55:03Z</dc:date>
    </item>
    <item>
      <title>Re: I would like to use a sas variable in Proc SQL from statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957111#M373657</link>
      <description>Thank You so much, managed to make it work. I am immensely grateful!</description>
      <pubDate>Fri, 24 Jan 2025 14:01:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-would-like-to-use-a-sas-variable-in-Proc-SQL-from-statement/m-p/957111#M373657</guid>
      <dc:creator>Jasuo</dc:creator>
      <dc:date>2025-01-24T14:01:25Z</dc:date>
    </item>
  </channel>
</rss>

